等待BackgroundWorker RunWorkerCompleted

发布于 2024-11-06 07:23:24 字数 877 浏览 4 评论 0原文

在主线程中,我有一个计时器。在 Tick 事件中,我运行一个 BackgroundWorker。我在那里做了一些事情,然后 BackgroundWorker 调用 RunWorkerCompleted 事件。

在主线程中,我有函数 Stop。此函数禁用定时器。但我想在 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;
}

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

谢谢

In the main thread I have a Timer. In the Tick event I run a BackgroundWorker. I do some things there and after that BackgroundWorker calls RunWorkerCompleted event.

In the main thread I have function Stop. This function disables the Timer. But I want wait for BackgroundWorker when he is working.

For example:

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

So my question is how wait for RunWorkerCompleted event of BackgroundWorker? I need to wait until DoSomethingElse(); is finished.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

新雨望断虹 2024-11-13 07:23:24

处理 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.
    }

}

Handle the BackgroundWorker.RunWorkerCompleted event which occures when the background operation has completed, has been canceled, or has raised an exception.

// 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.
    }

}
剪不断理还乱 2024-11-13 07:23:24

如果您只需要两个线程,请允许调用 this._backgroundWorker.RunWorkerAsync(); 的线程。
在调用此方法后死亡,并在 DoSomethingElse() 之后调用任何您想要发生的事情;在如下所示的同一块中

        private void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

DoSomethingElse();
DoSomethingAfterSomethingElse();

        }

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

If you only require two threads, allow the thread that called this._backgroundWorker.RunWorkerAsync();
to die after it calls this method and call anything you want to occur after DoSomethingElse(); within the same block as below

        private void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

DoSomethingElse();
DoSomethingAfterSomethingElse();

        }

Otherwise you are halting a thread to start another and then returning, which defeats the purpose of multiple threads?

你对谁都笑 2024-11-13 07:23:24

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

I think BackgroundWorker.IsBusy property is the only member that can help you in this case. Hope below logic will do what you need.

//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;
    }
} 
失与倦" 2024-11-13 07:23:24

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

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

Here is a way to stop/freeze the main thread until your background worker finishes:

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

Now if your application uses a form, I would just disable the whole form and show message letting the user know the the application is waiting for the process to finish. You can also have flag to disable the form from closing.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文