BackgroundWorker.DoWork イベント
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
RunWorkerAsync()が呼び出されたときに発生します。
public:
event System::ComponentModel::DoWorkEventHandler ^ DoWork;
public event System.ComponentModel.DoWorkEventHandler DoWork;
public event System.ComponentModel.DoWorkEventHandler? DoWork;
member this.DoWork : System.ComponentModel.DoWorkEventHandler
Public Custom Event DoWork As DoWorkEventHandler
イベントの種類
例
次のコード例は、 DoWork イベントを使用して非同期操作を開始する方法を示しています。 このコード例は、 BackgroundWorker クラスに提供されるより大きな例の一部です。
// This event handler is where the actual,
// potentially time-consuming work is done.
void backgroundWorker1_DoWork( Object^ sender, DoWorkEventArgs^ e )
{
// Get the BackgroundWorker that raised this event.
BackgroundWorker^ worker = dynamic_cast<BackgroundWorker^>(sender);
// Assign the result of the computation
// to the Result property of the DoWorkEventArgs
// object. This is will be available to the
// RunWorkerCompleted eventhandler.
e->Result = ComputeFibonacci( safe_cast<Int32>(e->Argument), worker, e );
}
// This event handler is where the actual,
// potentially time-consuming work is done.
void backgroundWorker1_DoWork(object sender,
DoWorkEventArgs e)
{
// Get the BackgroundWorker that raised this event.
BackgroundWorker worker = sender as BackgroundWorker;
// Assign the result of the computation
// to the Result property of the DoWorkEventArgs
// object. This is will be available to the
// RunWorkerCompleted eventhandler.
e.Result = ComputeFibonacci((int)e.Argument, worker, e);
}
' This event handler is where the actual work is done.
Private Sub backgroundWorker1_DoWork(
ByVal sender As Object,
ByVal e As DoWorkEventArgs) _
Handles backgroundWorker1.DoWork
' Get the BackgroundWorker object that raised this event.
Dim worker As BackgroundWorker =
CType(sender, BackgroundWorker)
' Assign the result of the computation
' to the Result property of the DoWorkEventArgs
' object. This is will be available to the
' RunWorkerCompleted eventhandler.
e.Result = ComputeFibonacci(e.Argument, worker, e)
End Sub
注釈
このイベントは、 RunWorkerAsync メソッドを呼び出すと発生します。 ここで、時間がかかる可能性のある作業を実行する操作を開始します。
DoWork イベント ハンドラーのコードでは、CancellationPending プロパティ値を定期的に確認し、true場合は操作を中止する必要があります。 この場合、System.ComponentModel.DoWorkEventArgsのCancel フラグをtrueに設定し、RunWorkerCompleted イベント ハンドラー内のSystem.ComponentModel.RunWorkerCompletedEventArgsのCancelled フラグをtrueに設定できます。
注意事項
DoWork イベント ハンドラー内のコードは、取り消し要求が行われると処理が完了する可能性があり、ポーリング ループがtrueに設定されているCancellationPendingを見逃す可能性があることに注意してください。 この場合、キャンセル要求が行われた場合でも、RunWorkerCompleted イベント ハンドラー内のSystem.ComponentModel.RunWorkerCompletedEventArgsのCancelled フラグはtrueに設定されません。 この状況は 競合状態 と呼ばれ、マルチスレッド プログラミングでは一般的な問題です。 マルチスレッド設計の問題の詳細については、「 マネージド スレッドのベスト プラクティス」を参照してください。
操作によって結果が生成される場合は、結果を DoWorkEventArgs.Result プロパティに割り当てることができます。 これは、RunWorkerCompletedEventArgs.Result プロパティのRunWorkerCompleted イベント ハンドラーで使用できます。
コードで処理されない例外が発生した場合、BackgroundWorkerは例外をキャッチし、RunWorkerCompleted イベント ハンドラーに渡します。このハンドラーは、System.ComponentModel.RunWorkerCompletedEventArgsのError プロパティとして公開されます。 Visual Studio デバッガーで実行している場合、未処理の例外が発生した DoWork イベント ハンドラーの時点でデバッガーが中断します。 複数の BackgroundWorkerがある場合は、 DoWork イベント ハンドラーを BackgroundWorkerの特定のインスタンスに結合するため、それらのいずれも直接参照しないでください。 代わりに、DoWork イベント ハンドラーで sender パラメーターをキャストして、BackgroundWorkerにアクセスする必要があります。
DoWork イベント ハンドラーでユーザー インターフェイス オブジェクトを操作しないように注意する必要があります。 代わりに、 BackgroundWorker イベントを介してユーザー インターフェイスと通信します。
イベントの処理方法の詳細については、「イベントの 処理と発生」を参照してください。