使用BackgroundWorker更新GUI

发布于 2024-10-20 16:25:19 字数 1196 浏览 0 评论 0原文

我一直在寻找并发现执行后台工作和更新 GUI 的一个好方法是使用后台工作人员。然而,执行这个(愚蠢的)小任务(从 1 计数到 10000)它不会更新标签内容,而是打印到调试! (当然,这只是另一个项目的尖峰解决方案......)

这是代码:

public partial class MainWindow : Window
{
    BackgroundWorker bw = new BackgroundWorker();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {

        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
        bw.WorkerReportsProgress = true;
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
        bw.RunWorkerAsync();

    }

    void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("DONE");

    }

    void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        label1.Content = "going here: "+e.ProgressPercentage;
        Debug.WriteLine(e.ProgressPercentage);
    }

    void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i=0; i < 10000; i++)
        {
            bw.ReportProgress((i*100)/10000);
        }
    }

}

I've been searching and found that a good way to perform background work and update the GUI is using background workers. However, doing this (stupid) little task (counting from 1 to 10000) it doesn't update the label content but prints to the debug! (This is just a spike solution for another project of course...)

Here's the code:

public partial class MainWindow : Window
{
    BackgroundWorker bw = new BackgroundWorker();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {

        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
        bw.WorkerReportsProgress = true;
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
        bw.RunWorkerAsync();

    }

    void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("DONE");

    }

    void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        label1.Content = "going here: "+e.ProgressPercentage;
        Debug.WriteLine(e.ProgressPercentage);
    }

    void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i=0; i < 10000; i++)
        {
            bw.ReportProgress((i*100)/10000);
        }
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

任谁 2024-10-27 16:25:19

ProgressChanged 事件在 UI 线程上引发,而不是在工作线程上引发。在您的代码中,工作线程几乎不执行任何操作(只是从 0 循环到 10000 并调用 ReportProgress),大部分工作都是在 UI 线程上完成的。基本上,您发送了太多进度通知。正因为如此,UI线程几乎总是很忙,没有时间渲染标签的新内容。

当您更改控件的属性时,WPF 中的渲染不会立即执行,而是在单独的调度程序框架上完成,当调度程序没有更紧急的事情要做时,会根据任务的优先级进行处理。用于渲染的优先级值为 7 (DispatcherPriority.Render); ProgressChanged 事件编组到优先级为 9 (DispatcherPriority.Normal) 的 UI 线程,如 MSDN 上指定的。因此,ProgressChanged 通知始终具有比渲染更高的优先级,并且由于它们不断出现,调度程序永远没有时间处理渲染任务。

如果您只是降低通知的频率,您的应用程序应该可以正常工作(目前您为每个百分比值发送 100 个通知,这是无用的):

    void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 0; i < 10000; i++)
        {
            if (i % 100 == 0)
                bw.ReportProgress(i / 100);
        }
    }

The ProgressChanged event is raised on the UI thread, not the worker thread. In your code, the worker thread is doing almost nothing (just loop from 0 to 10000 and call ReportProgress), most of the work is done on the UI thread. Basically, you're sending too many progress notifications. Because of this, the UI thread is almost always busy and has no time to render the new content of the label.

Rendering in WPF is not performed immediately when you change a property of a control, it is done on a separate dispatcher frame, which is processed when the dispatcher has nothing more urgent to do, based on the priority of the task. The priority used for rendering has a value of 7 (DispatcherPriority.Render); the ProgressChanged event is marshalled to the UI thread with a priority of 9 (DispatcherPriority.Normal), as specified on MSDN. So the ProgressChanged notifications always have a higher priority than rendering, and since they keep coming, the dispatcher never has time to process the rendering tasks.

If you just decrease the frequency of the notifications, your app should work fine (currently you're sending 100 notifications for each percentage value, which is useless):

    void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 0; i < 10000; i++)
        {
            if (i % 100 == 0)
                bw.ReportProgress(i / 100);
        }
    }
夏末的微笑 2024-10-27 16:25:19
this.Dispatcher.BeginInvoke( (Action) delegate(){
   label1.Content = "going here: "+e.ProgressPercentage;
});
this.Dispatcher.BeginInvoke( (Action) delegate(){
   label1.Content = "going here: "+e.ProgressPercentage;
});
白馒头 2024-10-27 16:25:19

尝试使用如下内容更改标签:

string Text = "going here: " + e.ProgressPercentage;
this.Invoke((MethodInvoker)delegate {
    label1.Content = newText;
});

请注意,我不确定它是否有效。我现在无法测试。如果不起作用,请告诉我,我将删除答案。

如果您需要一种规范的方法来完成您想要的操作,请查看这篇文章中的 Hath 答案:如何从另一个线程更新 GUI?

Try to change the label using womething like this:

string Text = "going here: " + e.ProgressPercentage;
this.Invoke((MethodInvoker)delegate {
    label1.Content = newText;
});

Note that i'm not sure it will work. I can not test it now. If it does not work, let me know and I will delete the answer.

If you need the a canonical way to do exactly what you want, look at the Hath answer in this post: How do I update the GUI from another thread?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文