4

我有一个 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;
    }
4

1 回答 1

8

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

[编辑]:更新business class了用户的评论

注意:我的表单类被命名Form3。你可能需要改变你的Program.cs or vice-versa.

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& 然后我_worker.CancellationPending在 DoWork 中检查。因此,如果您想通过 a 取消该过程Cancel Button click,那么您将在按钮处理程序中编写此代码-_worker.CancelAsync();

于 2012-06-14T12:37:56.587 回答