C#/WPF - I can't update UI from a backgroundworker

后端 未结 1 1164
余生分开走
余生分开走 2021-01-15 01:10

I have a code that fetches tweets from a specific Twitter account using Tweetsharp library, creates instance of a custom UserControl and post t

相关标签:
1条回答
  • 2021-01-15 02:00

    You get this exception because your background worker uses a new thread, and this thread is different than the main UI thread. To simplify the error message says that you cannot change your UI element from another thread, they are independant.

    This answer will solve your problem.

    I also found this answer from @Marc Gravell

    ///...blah blah updating files
    string newText = "abc"; // running on worker thread
    this.Invoke((MethodInvoker)delegate {
        someLabel.Text = newText; // runs on UI thread
    });
    ///...blah blah more updating files
    
    0 讨论(0)
提交回复
热议问题