BackgroundWorker.RunWorkerCompleted 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
當背景操作已完成、已被取消或提出例外時,會發生這種情況。
public:
event System::ComponentModel::RunWorkerCompletedEventHandler ^ RunWorkerCompleted;
public event System.ComponentModel.RunWorkerCompletedEventHandler RunWorkerCompleted;
public event System.ComponentModel.RunWorkerCompletedEventHandler? RunWorkerCompleted;
member this.RunWorkerCompleted : System.ComponentModel.RunWorkerCompletedEventHandler
Public Custom Event RunWorkerCompleted As RunWorkerCompletedEventHandler
事件類型
範例
以下程式碼範例展示了如何利用 RunWorkerCompleted 事件處理非同步操作的結果。 此程式碼範例是本類別更大範例 BackgroundWorker 的一部分。
// This event handler deals with the results of the
// background operation.
void backgroundWorker1_RunWorkerCompleted( Object^ /*sender*/, RunWorkerCompletedEventArgs^ e )
{
// First, handle the case where an exception was thrown.
if ( e->Error != nullptr )
{
MessageBox::Show( e->Error->Message );
}
else
if ( e->Cancelled )
{
// Next, handle the case where the user cancelled
// 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.
resultLabel->Text = "Cancelled";
}
else
{
// Finally, handle the case where the operation
// succeeded.
resultLabel->Text = e->Result->ToString();
}
// Enable the UpDown control.
this->numericUpDown1->Enabled = true;
// Enable the Start button.
startAsyncButton->Enabled = true;
// Disable the Cancel button.
cancelAsyncButton->Enabled = false;
}
// This event handler deals with the results of the
// background operation.
void backgroundWorker1_RunWorkerCompleted(
object sender, RunWorkerCompletedEventArgs e)
{
// First, handle the case where an exception was thrown.
if (e.Error != null)
{
_ = MessageBox.Show(e.Error.Message);
}
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.
resultLabel.Text = "Canceled";
}
else
{
// Finally, handle the case where the operation
// succeeded.
resultLabel.Text = e.Result.ToString();
}
// Enable the UpDown control.
numericUpDown1.Enabled = true;
// Enable the Start button.
startAsyncButton.Enabled = true;
// Disable the Cancel button.
cancelAsyncButton.Enabled = false;
}
' This event handler deals with the results of the
' background operation.
Private Sub backgroundWorker1_RunWorkerCompleted(
ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) _
Handles backgroundWorker1.RunWorkerCompleted
' First, handle the case where an exception was thrown.
If (e.Error IsNot Nothing) Then
MessageBox.Show(e.Error.Message)
ElseIf e.Cancelled Then
' 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.
resultLabel.Text = "Canceled"
Else
' Finally, handle the case where the operation succeeded.
resultLabel.Text = e.Result.ToString()
End If
' Enable the UpDown control.
numericUpDown1.Enabled = True
' Enable the Start button.
startAsyncButton.Enabled = True
' Disable the Cancel button.
cancelAsyncButton.Enabled = False
End Sub
備註
當 DoWork 事件處理者返回時,此事件會被提出。
如果操作成功完成且其結果被指派到 DoWork 事件處理器中,你可以透過屬性 RunWorkerCompletedEventArgs.Result 存取該結果。
Error屬性System.ComponentModel.RunWorkerCompletedEventArgs表示該操作拋出了例外。
Cancelled屬性 表示System.ComponentModel.RunWorkerCompletedEventArgs取消請求是否被背景操作處理。 如果你在事件處理程式中DoWork偵測到取消請求,並CancellationPending檢查旗標並將 的System.ComponentModel.DoWorkEventArgs旗標設Cancel為 true,Cancelled則 的System.ComponentModel.RunWorkerCompletedEventArgs旗標也會被設為 true。
謹慎
請注意,事件處理程式中的 DoWork 程式碼可能會在取消請求中完成工作,輪詢迴圈可能會錯過 CancellationPending 設定為 true。 在這種情況下,Cancelled你的RunWorkerCompleted事件處理器中的 旗System.ComponentModel.RunWorkerCompletedEventArgs標不會被設為 true,儘管已經提出取消請求。 這種情況稱為 競賽條件 ,是多執行緒程式設計中常見的問題。 欲了解更多多執行緒設計問題,請參閱管理執行任務最佳實務。
你的RunWorkerCompleted事件處理員應該在存取RunWorkerCompletedEventArgs.Result屬性前先檢查 AsyncCompletedEventArgs.Error and AsyncCompletedEventArgs.Cancelled 屬性。 若提出例外或操作被取消,存取 RunWorkerCompletedEventArgs.Result 該屬性會產生例外。
適用於
另請參閱
- 如何:在背景執行操作
- 如何:在背景下載檔案