WPF C# Task run method System.InvalidOperationException: 'The calling thread must be STA, because many UI components require this.'

Task.Run(() =>
{
    InitData();
});

The calling thread must be STA, because many UI components require this.

 

The solution is make sure current method in current main thread

 Application.Current.Dispatcher.BeginInvoke(new Action(() =>
 {
     InitData();
 }));

 

posted @ 2024-08-28 14:32  FredGrit  阅读(31)  评论(0)    收藏  举报