简体   繁体   English

c#如何使用BackgroundWorker的ProgressChanged事件在线程中间做UI动作

[英]c# how to use ProgressChanged event of BackgroundWorker to do UI actions in the middle of thread

Here is simple code which has UI actions in the middle of backgroundworker.这是一个简单的代码,它在后台工作人员的中间有 UI 操作。 There is a form and a label "lbProgress" on it.上面有一个表格和一个标签“lbProgress”。 When ProgressPercentage = 2, label is not changed but the action does.当 ProgressPercentage = 2 时,标签不会更改,但操作会更改。

    private void Form1_Load(object sender, EventArgs e)
    {
        bw.WorkerSupportsCancellation = true;
        bw.WorkerReportsProgress = true;
        bw.DoWork += bw_DoWork;
        bw.ProgressChanged += bw_ProgressChanged;
        bw.RunWorkerCompleted += bw_RunWorkerCompleted;
    }
    int ii;
    private void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        ii = 0;

        bw.ReportProgress(1);
        ii += 10;
        Thread.Sleep(1000);

        bw.ReportProgress(2); // here intended to do UI actions

        bw.ReportProgress(3);
        ii += 20;
        Thread.Sleep(1000);
    }
    private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show(ii.ToString());
    }
    private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        lbProgress.Text = e.ProgressPercentage.ToString();
        if (e.ProgressPercentage == 2)
        {
            this.Text += ", 00";
            ii += 100;
            Thread.Sleep(1000);
        }
    }

    private void btRun_Click(object sender, EventArgs e)
    {
        bw.RunWorkerAsync();
    }

Is it even possible to do large UI actions in the middle of backgroundworker thread using ProgressChanged event?甚至可以使用 ProgressChanged 事件在后台工作线程中间执行大型 UI 操作吗?

This is my answer to your question "Why do I see this behaviour?":这是我对您的问题“为什么我会看到这种行为?”的回答:

From the documentation of ReportProgress :ReportProgress的文档中:

The call to the ReportProgress method is asynchronous and returns immediately.对 ReportProgress 方法的调用是异步的并立即返回。

So what happens is:所以会发生什么:

  • The BackgroundWorker calls ReportProgress(2) BackgroundWorker 调用 ReportProgress(2)
  • The ProgressChanged event handler for progress 2 is started on the UI thread and blocks.进度 2 的 ProgressChanged 事件处理程序在 UI 线程和块上启动。 The UI is not updated because the UI thread is blocked . UI 未更新,因为 UI 线程被阻塞 (Hint: the screen is redrawn usually only when the UI thread is idle) (提示:通常只有在 UI 线程空闲时才会重绘屏幕)
  • In the meantime, the BackgroundWorker has called ReportProgress(3)与此同时,BackgroundWorker 调用了 ReportProgress(3)
  • The Blocked event handler finally finishes Blocked 事件处理程序终于完成
  • The ProgressChanged event handler for progress 3 is run updating lbProgress.Text to "3" before your eyes had a chance to notice that it was "2" just a microsecond before (if it was rendered at all).进度 3 的 ProgressChanged 事件处理程序正在运行,将 lbProgress.Text 更新为“3”,然后您的眼睛有机会注意到它在一微秒前是“2”(如果它被渲染的话)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 后台工作人员进度更改事件C# - Backgroundworker progresschanged event c# 在C#winforms中,为什么UI标签没有更新并跟上backgroundworker progresschanged事件? - In C# winforms, why is the UI label not updating and keeping up with backgroundworker progresschanged event? (C#) BackgroundWorker() ProgressChanged 不工作 - (C#) BackgroundWorker() ProgressChanged not working 我如何在 backgroundworker dowork 事件中使用 WebClient progresschanged 事件? - How do I use the WebClient progresschanged event from within backgroundworker dowork event? 我需要在backgroundworker progresschanged事件中使用invoke吗? - Do I need to use invoke in backgroundworker progresschanged event? C#BackgroundWorker - 最好将结果保存到文件本身还是使用ProgressChanged? - C# BackgroundWorker - better to save results to a file itself or use ProgressChanged? BackgroundWorker.ProgressChanged事件不在Dispatcher Thread上 - BackgroundWorker.ProgressChanged event not on Dispatcher Thread c# backgroundWorker 未引发 ProgressChanged 或 RunWorkerCompleted 事件 - c# backgroundWorker not raising ProgressChanged or RunWorkerCompleted events 如何在C#中使用BackgroundWorker类使用多线程? - How to use multi-thread using BackgroundWorker class in C#? 在调用ReportProgress之后,不会立即触发C#BackgroundWorker.ProgressChanged - C# BackgroundWorker.ProgressChanged not fired immediately after ReportProgress is called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM