如何使用BackgroundWorker事件RunWorkerCompleted

时间:2013-11-01 09:07:07

标签: c# winforms backgroundworker

全部,我已经知道BackgroundWorker在WinForm中处理多线程案例的基本用法。代码结构如下所示。

在应用程序的主线程中。刚刚启动BackgroundWork。

    if (backgroundWorker1.IsBusy != true)
    {
        // Start the asynchronous operation.
        backgroundWorker1.RunWorkerAsync();
    }

然后它将触发DoWork事件。所以我们可以在那里做点什么。

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker;
    ......
    // report progress.
    worker.ReportProgress(iProgress);
    ....
}

然后我们只需处理ProgressChanged事件以显示BackgroundWorker进度。

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    //show progress.   
    resultLabel.Text = (e.ProgressPercentage.ToString() + "%");
}

DoWork完成或发生异常后。事件RunWorkerCompleted将被解雇。

这是我对这个事件处理的问题。请帮助审查它们。感谢。

我注意到RunWorkerCompletedEventArgs e中有一个名为'Result'的属性,它用于什么?我该如何使用它?

是否有可能接收我的自定义异常消息而不是e.error?如果有,如何制作?

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Cancelled == true)
    {
        resultLabel.Text = "Canceled!";
    }
    else if (e.Error != null)
    {
        resultLabel.Text = "Error: " + e.Error.Message;
    }
    else
    {
        resultLabel.Text = e.Result.ToString();
    }
}

3 个答案:

答案 0 :(得分:14)

Result中的RunWorkerCompletedEventArgs属性是您在DoWorkEventHandler中分配给DoWork()的结果属性的值。

您可以为此分配任何您喜欢的内容,因此您可以返回整数,字符串,对象/复合类型等。

如果在DoWork()中抛出异常,则可以在Error的{​​{1}}属性中访问该异常。在这种情况下,访问Result属性将导致抛出RunWorkerCompletedEventArgs

答案 1 :(得分:6)

public class MyWorkerClass
{
    private string _errorMessage = "";
    public string ErrorMessage { get { return _errorMessage; } set { _errorMessage = value; }}

    public void RunStuff(object sender, DoWorkEventArgs e)
    {
        //... put some code here and fill ErrorMessage whenever you want
    }
}

然后是你使用它的类

public class MyClassUsingWorker
{
    // have reference to the class where the worker will be running
    private MyWorkerClass mwc = null;

    // run the worker
    public void RunMyWorker()
    {
        mwc = new MyWorkerClass();
        BackgroundWorker backgroundWorker1 = new BackgroundWorker();
        backgroundWorker1.DoWork += new DoWorkEventHandler(mwc.RunStuff);
        backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
        backgroundWorker1.RunWorkerAsync();
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        string strSpecialMessage = mwc.ErrorMessage;

        if (e.Cancelled == true)
        {
            resultLabel.Text = "Canceled!";
        }
        else if (e.Error != null)
        {
            resultLabel.Text = "Error: " + e.Error.Message;
        }
        else
        {
            resultLabel.Text = e.Result.ToString();
        }
    }
}

答案 2 :(得分:0)

您可以使用Result属性存储来自DoWork的任何结果,并从Completed事件中访问它。但是,如果后台工作进程被取消或引发异常,则无法访问结果。您会找到更多详细信息here