2011-05-07 114 views
2

在主線程中,我有一個Timer。在Tick事件中,我運行BackgroundWorker。我在那裏做了一些事情,之後BackgroundWorker調用RunWorkerCompleted事件。等待BackgroundWorker RunWorker完成

在主線程中我有功能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();完成。

感謝

+2

沒有。你不會等待後臺工作人員......這就是爲什麼它叫做後臺工作人員 - 你的應用程序在後臺工作人員正在工作時繼續進行工作。您將一個事件監聽器附加到您的應用程序,應該在後臺工作人員完成後觸發,然後處理數據。 – ppumkin 2011-05-07 13:46:23

+0

ariba! http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx – ppumkin 2011-05-07 13:47:12

+0

私人小組backgroundWorker1_RunWorkerCompleted(_ BYVAL發件人作爲對象,BYVALË作爲RunWorkerCompletedEventArgs)_ 把手backgroundWorker1.RunWorkerCompleted – ppumkin 2011-05-07 13:48:39

回答

1

如果你只需要兩個線程,讓那個叫this._backgroundWorker.RunWorkerAsync線程();在調用此方法並在DoSomethingElse()之後調用任何想要發生的事件後,它將死亡。相同的塊如下

 private void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
     { 

DoSomethingElse(); 
DoSomethingAfterSomethingElse(); 

     } 

否則,你就停止線程啓動另一個,然後再返回,這違背了多線程的目的範圍內?

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

我認爲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; 
    } 
} 
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; 
}