等待BackgroundWorker RunWorkerCompleted

时间:2011-05-07 13:43:41

标签: c# .net multithreading backgroundworker

在主线程中我有一个Timer。在Tick活动中,我运行了BackgroundWorker。我在那里做了一些事情,之后BackgroundWorker调用RunWorkerCompleted事件。

在主线程中我有函数Stop。此功能禁用Timer。但我想在他工作的时候等BackgroundWorker

例如:

public void Next()
{

    // Start the asynchronous operation
    if (!this._backgroundWorker.IsBusy)
        this._backgroundWorker.RunWorkerAsync();

}

private void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    DoSomething();

}


private void _backgroundWorker_RunWorkerCompleted(object sender, 
    RunWorkerCompletedEventArgs e)
{
    DoSomethingElse();
}

public void Stop()
{
    this._timer.Enabled = false;
}

所以我的问题是如何等待RunWorkerCompleted BackgroundWorker事件?我需要等到DoSomethingElse();完成。

由于

4 个答案:

答案 0 :(得分:2)

处理后台操作完成,取消或引发异常时发生的BackgroundWorker.RunWorkerCompleted事件。

// This event handler deals with the results of the
// background operation.

private void backgroundWorker1_RunWorkerCompleted(
    object sender, RunWorkerCompletedEventArgs e)
{
    // First, handle the case where an exception was thrown.
    if (e.Error != null)
    {
    }
    else if (e.Cancelled)
    {
        // Next, handle the case where the user canceled 
        // the operation.
        // Note that due to a race condition in 
        // the DoWork event handler, the Cancelled
        // flag may not have been set, even though
        // CancelAsync was called.
    }
    else
    {
        // Finally, handle the case where the operation 
        // succeeded.
    }

}

答案 1 :(得分:1)

如果您只需要两个线程,请允许调用this._backgroundWorker.RunWorkerAsync()的线程; 在调用此方法并在DoSomethingElse()之后调用您想要发生的任何内容之后死掉;在同一个区块内

        private void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

DoSomethingElse();
DoSomethingAfterSomethingElse();

        }

否则你停止一个线程来启动另一个线程然后返回,这会破坏多线程的目的?

答案 2 :(得分:1)

我认为在这种情况下,BackgroundWorker.IsBusy属性是唯一可以帮助您的成员。希望低于逻辑将做你需要的。

//Add a class member
private bool stopped;

public void Stop() 
{    
     if (!this._backgroundWorker.IsBusy)
     {
         this._timer.Enabled = false; 
         stopped = false;
     }
     else
     {
         stopped = true;
     }

} 

private void _backgroundWorker_RunWorkerCompleted(object sender,      RunWorkerCompletedEventArgs e) 
{     
     DoSomethingElse(); 

    if (stopped)
    {
             this._timer.Enabled = false; 
             stopped = false;
    }
} 

答案 3 :(得分:0)

这是一种停止/冻结主线程的方法,直到你的后台工作者完成:

public void Stop()
{
    if (!_backgroundWorker.IsBusy)
    {
        _timer.Enabled = false;

        // Stop/Freeze the main thread until the background worker finishes 
        while (_backgroundWorker.IsBusy)
        {
            Thread.Sleep(100);
        }
    }
}

现在,如果您的应用程序使用表单,我将禁用整个表单并显示消息,让用户知道应用程序正在等待该过程完成。您还可以使用标记来禁用关闭表单。

private bool _canClose;

public void Stop()
{
    if (!_backgroundWorker.IsBusy)
    {
        _timer.Enabled = false;
        // Don't let the user do anything in the form until the background worker finishes
        this.IsEnabled = false;
        _label.Text = "Waiting for the process to finish";
        _canClose = false; 
    }
 }
private void _backgroundWorker_RunWorkerCompleted(object sender,
    RunWorkerCompletedEventArgs e)
{
    DoSomethingElse();
    // Allow the user to close the form
    this.IsEnabled = true;
    _canClose = true;
}

private void MainWindow_Closing(object sender, CancelEventArgs e)
{
    e.Cancel = !_canClose;
}