BackgroundWorker.RunWorkerCompleted イベント
アセンブリ: System (system.dll 内)

Dim instance As BackgroundWorker Dim handler As RunWorkerCompletedEventHandler AddHandler instance.RunWorkerCompleted, handler
public: event RunWorkerCompletedEventHandler^ RunWorkerCompleted { void add (RunWorkerCompletedEventHandler^ value); void remove (RunWorkerCompletedEventHandler^ value); }

このイベントは、DoWork イベント ハンドラから戻るときに発生します。
操作が正常に完了し、その結果が DoWork イベント ハンドラに割り当てられると、RunWorkerCompletedEventArgs.Result プロパティからその結果にアクセスできます。
System.ComponentModel.RunWorkerCompletedEventArgs の Error プロパティは、操作によって例外がスローされたことを示します。
System.ComponentModel.RunWorkerCompletedEventArgs の Cancelled プロパティは、バックグラウンド操作によってキャンセル要求が処理されたかどうかを示します。DoWork イベント ハンドラ内のコードが CancellationPending フラグをチェックし、System.ComponentModel.DoWorkEventArgs の Cancel フラグを true に設定することによってキャンセル要求を検出した場合、System.ComponentModel.RunWorkerCompletedEventArgs の Cancelled フラグも true に設定されます。
![]() |
---|
DoWork イベント ハンドラ内のコードは、キャンセル要求が行われているときに処理を終了できること、また、ポーリング ループは、CancellationPending に true を設定し損ねる場合があることに注意してください。この場合、キャンセル要求が行われていても、RunWorkerCompleted イベント ハンドラの System.ComponentModel.RunWorkerCompletedEventArgs の Cancelled フラグは true に設定されません。この状況は競合状態と呼ばれ、マルチスレッドのプログラミングにおける一般的な懸念事項です。マルチスレッド デザインに関する問題の詳細については、「マネージ スレッド処理の実施」を参照してください。 |
RunWorkerCompleted イベント ハンドラは、RunWorkerCompletedEventArgs.Result プロパティにアクセスする前に、常に AsyncCompletedEventArgs.Error プロパティおよび AsyncCompletedEventArgs.Cancelled プロパティをチェックする必要があります。例外が発生した場合、または操作がキャンセルされた場合、RunWorkerCompletedEventArgs.Result プロパティにアクセスすると例外が発生します。

RunWorkerCompleted イベントを使用して、非同期操作の結果を処理するコード例を次に示します。このコード例は、BackgroundWorker クラスのトピックで取り上げているコード例の一部分です。
' 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 Not (e.Error Is 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. Me.numericUpDown1.Enabled = True ' Enable the Start button. startAsyncButton.Enabled = True ' Disable the Cancel button. cancelAsyncButton.Enabled = False End Sub 'backgroundWorker1_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) { 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. 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 != 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. private void backgroundWorker1_RunWorkerCompleted(Object sender, RunWorkerCompletedEventArgs e) { // First, handle the case where an exception was thrown. if (e.get_Error() != null) { MessageBox.Show(e.get_Error().get_Message()); } else { if (e.get_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.set_Text("Cancelled"); } else { // Finally, handle the case where the operation // succeeded. resultLabel.set_Text(e.get_Result().ToString()); } } // Enable the UpDown control. this.numericUpDown1.set_Enabled(true); // Enable the Start button. startAsyncButton.set_Enabled(true); // Disable the Cancel button. cancelAsyncButton.set_Enabled(false); } //backgroundWorker1_RunWorkerCompleted

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からBackgroundWorker.RunWorkerCompleted イベントを検索する場合は、下記のリンクをクリックしてください。

- BackgroundWorker.RunWorkerCompleted イベントのページへのリンク