Note that there are some explanatory texts on larger screens.

plurals
  1. POC#/WPF - I can't update UI from a backgroundworker
    primarykey
    data
    text
    <p>I have a code that fetches tweets from a specific Twitter account using <strong>Tweetsharp</strong> library, creates instance of a custom <code>UserControl</code> and post tweet text to that <code>UserControl</code> then add it to a <code>StackPanel</code>.</p> <p>However, I have to get a lot of tweets and it seems that the application would freeze while adding user controls to the <code>StackPanel</code>. I tried using <code>BackgroundWorker</code>, but I wasn't lucky until now.</p> <p>My code :</p> <pre><code>private readonly BackgroundWorker worker = new BackgroundWorker(); // This ( UserControl ) is used in the MainWindow.xaml private void UserControl_Loaded_1(object sender, RoutedEventArgs e) { worker.DoWork += worker_DoWork; worker.RunWorkerCompleted += worker_RunWorkerCompleted; worker.RunWorkerAsync(); } private void worker_DoWork(object sender, DoWorkEventArgs e) { int usrID; var service = new TwitterService(ConsumerKey, ConsumerSecret); service.AuthenticateWith(AccessToken, AccessTokenSecret); ListTweetsOnUserTimelineOptions options = new ListTweetsOnUserTimelineOptions(); options.UserId = usrID; options.IncludeRts = true; options.Count = 10; twitterStatuses = service.ListTweetsOnUserTimeline(options); } private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { try { foreach (var item in twitterStatuses) { TweetViewer tweetViewer = new TweetViewer(); // A UserControl within another UserControl tweetViewer.Tweet = item.Text; tweetViewer.Username = "@stackoverflow"; tweetViewer.RealName = "Stack Overflow" tweetViewer.Avatar = ImageSourcer(item.Author.ProfileImageUrl); stackPanel.Children.Add(tweetViewer); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } </code></pre> <p>What I want to do now is to solve the problem of not being able to perform the code contained in <strong>worker_RunWorkerCompleted</strong> within a <code>BackgroundWorker</code> but every time I try to perform it using a <code>BackgroundWorker</code> it fails &amp; gives me errors like :</p> <blockquote> <p>The calling thread must be STA, because many UI components require this.</p> </blockquote> <p>I tried also using a <strong>STA System.Threading.Thread</strong> instead of the BackgroundWorker but without luck!</p> <p>What am I missing ? I'm really new to WPF and I may be ignoring something important.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload