等待后台工作线程运行工作线程完成

本文关键字:工作 线程 运行 等待 后台 | 更新日期: 2023-09-27 17:55:53

在主线程中,我有一个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();完成。

谢谢

等待后台工作线程运行工作线程完成

处理后台操作已完成、已取消或引发异常时发生的 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.
    }
}

如果只需要两个线程,则允许调用 this._backgroundWorker.RunWorkerAsync() 的线程;在它调用此方法后死亡,并在 DoSomethingElse() 之后调用您想要发生的任何事情;在与下面相同的块内

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

否则,您将停止一个线程以启动另一个线程然后返回,这违背了多个线程的目的?

我认为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;
    }
} 

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

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;
}