winforms - 从后台线程更新 UI,当线程完成时,该线程在主 UI 中循环调用

标签 winforms multithreading

我有一个 WinForms 应用程序,它正在调用一个业务类方法,该方法执行一些重型操作,每次调用大约需要 5 秒。主窗体循环调用此方法。该循环可以运行 10 次到最多 10,000 次。

WinForms 应用程序向业务类发送一个参数,并有一个区域显示每个方法调用所花费的时间以及该方法返回的值。如何通知我的主窗口并使用该方法为每次调用返回的内容更新主 winform 中的文本区域?

目前,数据是在所有线程完成后一次性全部到达的。有没有办法在每次调用完成后更新循环所有迭代的 UI?我不介意它是否也按顺序完成。

表格

    HeavyDutyClass hd;
    public Form1()
    {
        InitializeComponent();
        hd = new HeavyDutyClass();
    }


    //BUTTON CLICK
    private void Start_Click(object sender, EventArgs e)
    {

        int filecount = 5000;  //BAD - opening 5000 threads! Any other approach?
        hd.FileProcessed += new EventHandler(hd_FileProcessed);


        var threads = new Thread[filecount];

        for (int i = 0; i < filecount; i++)
        {
            threads[i] = new Thread(() => { hd.LongRunningMethod(); });
            threads[i].Start();   
        }

    }

    //BUSINESS CLASS EVENT THAT FIRES WHEN BUSINESS METHOD COMPELTES
    void hd_FileProcessed(object sender, EventArgs e)
    {

        if (dgv.InvokeRequired)
        {
            dgv.Invoke((MethodInvoker)delegate { UpdateGrid(); });

        }
    }

    private void UpdateGrid()
    {
        dgv.Rows.Add(1);
        int i = dgv.Rows.Count;
        dgv.Rows [ i-1].Selected = true;
        dgv.FirstDisplayedScrollingRowIndex = i - 1;

    }

商务重载级

    public event EventHandler FileProcessed;

    public HeavyDutyClass()
    {
    }

    protected virtual void OnMyEvent(EventArgs e)
    {
        if (FileProcessed != null)
        {
            FileProcessed(this, e);
        }
    }

    public bool LongRunningMethod()
    {
        for (double i = 0; i < 199990000; i++)
        {
            //time consuming loop
        }
        OnMyEvent(EventArgs.Empty);
        return true;
    }

最佳答案

添加一个 Winforms 项目,在表单上放置一个标签控件,复制粘贴此代码并按 F5

[编辑]:使用用户的商务舱评论进行了更新

注意:我的表单类名为 Form3。您可能必须更改您的Program.cs,反之亦然。

using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class BusinessClass
    {
        public int MyFunction(int input)
        {
            return input+10;
        }
    }

    public partial class Form3 : Form
    {
        private BackgroundWorker _worker;
        BusinessClass _biz = new BusinessClass();
        public Form3()
        {
            InitializeComponent();
            InitWorker();
        }

        private void InitWorker()
        {
            if (_worker != null)
            {
                _worker.Dispose();
            }

            _worker = new BackgroundWorker
            {
                WorkerReportsProgress = true,
                WorkerSupportsCancellation = true
            };
            _worker.DoWork += DoWork;
            _worker.RunWorkerCompleted += RunWorkerCompleted;
            _worker.ProgressChanged += ProgressChanged;
            _worker.RunWorkerAsync();
        }


        void DoWork(object sender, DoWorkEventArgs e)
        {
            int highestPercentageReached = 0;
            if (_worker.CancellationPending)
            {
                e.Cancel = true;
            }
            else
            {
                double i = 0.0d;
                int junk = 0;
                for (i = 0; i <= 199990000; i++)
                {
                    int result = _biz.MyFunction(junk);
                    junk++;

                    // Report progress as a percentage of the total task.
                    var percentComplete = (int)(i / 199990000 * 100);
                    if (percentComplete > highestPercentageReached)
                    {
                        highestPercentageReached = percentComplete;
                        // note I can pass the business class result also and display the same in the LABEL  
                        _worker.ReportProgress(percentComplete, result);
                        _worker.CancelAsync();
                    }
                }

            }
        }

        void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                // Display some message to the user that task has been
                // cancelled
            }
            else if (e.Error != null)
            {
                // Do something with the error
            }
        }

        void ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            label1.Text =  string.Format("Result {0}: Percent {1}",e.UserState, e.ProgressPercentage);
        }
    }
}

有了这个,您也可以非常轻松地实现取消功能。 请注意,在初始化过程中,我设置了 WorkerSupportsCancellation = true,然后在 DoWork 中检查 _worker.CancellationPending。因此,如果您想通过单击取消按钮来取消该进程,那么您将在按钮处理程序中编写此代码 - _worker.CancelAsync();

关于winforms - 从后台线程更新 UI,当线程完成时,该线程在主 UI 中循环调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11032333/

相关文章:

c - 使用 Pthread 的 C 语言基本多线程程序说明

C# 线程,如何让线程运行带有参数的方法?

java - 理解死锁、同步

c# - 如何对用户控件中公开的属性使用内置编辑器 - Mask Property Editor Issue

c# - WinForms:我应该总是注销事件回调吗?

c# - 关闭第二个窗体后将文本框聚焦在主窗体上

c++ - 为无锁队列添加阻塞函数

.net - DisplaySettingsChanging 上的 WPF 互操作死锁

c# - .Net Windows Forms ListView OwnerDraw LargeImageList 泄漏

java - JLabel setForeground 在 SwingWorker 中不起作用