C#/ WPF - 我不能从一个BackgroundWorker更新UI [英] C#/WPF - I can't update UI from a backgroundworker

查看:515
本文介绍了C#/ WPF - 我不能从一个BackgroundWorker更新UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,使用的 Tweetsharp 库从一个特定的Twitter帐户取鸣叫代码,创建一个自定义的实例,用户控件和后鸣叫文本该用户控件然后将其添加到的StackPanel

I have a code that fetches tweets from a specific Twitter account using Tweetsharp library, creates instance of a custom UserControl and post tweet text to that UserControl then add it to a StackPanel.

不过,我得到了很多的鸣叫,似乎在添加用户控件到的StackPanel 申请将冻结。我试着用的BackgroundWorker ,但我不幸运,到现在为止

However, I have to get a lot of tweets and it seems that the application would freeze while adding user controls to the StackPanel. I tried using BackgroundWorker, but I wasn't lucky until now.

我的代码:

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);
    }

}



我想现在要做的是解决不能够执行包含在 worker_RunWorkerCompleted 中的的BackgroundWorker ,但每次我试图使用执行它<$代码的问题C $ C>的BackgroundWorker 失败和放大器;给我的错误,如:

What I want to do now is to solve the problem of not being able to perform the code contained in worker_RunWorkerCompleted within a BackgroundWorker but every time I try to perform it using a BackgroundWorker it fails & gives me errors like :

调用线程必须STA,因为许多UI组件都需要这

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

我也试过用 STA System.Threading.Thread 而不是BackgroundWorker的,但没有运气!

I tried also using a STA System.Threading.Thread instead of the BackgroundWorker but without luck!

我在想什么?我真的很新的WPF,我可能忽略了一些重要的东西。

What am I missing ? I'm really new to WPF and I may be ignoring something important.

推荐答案

您,因为您的背景工人使用这种例外新线程,该线程比主UI线程不同。
为了简化错误消息说,你可以不是你的UI元素从另一个线程改变,他们是独立的。

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.

这的answer~~V 将解决你的问题。

我也发现了这个回答从@Marc Gravell

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

这篇关于C#/ WPF - 我不能从一个BackgroundWorker更新UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆