BackgroundWorker.DoWork イベント

定義

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.
private 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フラグを CanceltrueCancelled設定できます。イベント ハンドラーの のSystem.ComponentModel.RunWorkerCompletedEventArgsRunWorkerCompletedフラグは にtrue設定されます。

注意事項

イベント ハンドラー内のDoWorkコードは、取り消し要求が行われると処理が完了する可能性があり、ポーリング ループが にtrue設定されていない可能性があることにCancellationPending注意してください。 この場合、キャンセル要求がCancelled行われた場合でも、イベント ハンドラー内RunWorkerCompletedの の System.ComponentModel.RunWorkerCompletedEventArgs フラグは にtrue設定されません。 この状況は 競合状態 と呼ばれ、マルチスレッド プログラミングでは一般的な懸念事項です。 マルチスレッド設計の問題の詳細については、「 マネージド スレッドのベスト プラクティス」を参照してください。

操作によって結果が生成される場合は、結果を プロパティに DoWorkEventArgs.Result 割り当てることができます。 これは、 プロパティの RunWorkerCompleted イベント ハンドラーで RunWorkerCompletedEventArgs.Result 使用できます。

操作によって、コードで処理されない例外が発生した場合、 BackgroundWorker によって例外がキャッチされ、イベント ハンドラーにRunWorkerCompleted渡されます。ここで、 の System.ComponentModel.RunWorkerCompletedEventArgsプロパティとしてError公開されます。 Visual Studio デバッガーで実行している場合、ハンドルされない例外が発生したイベント ハンドラーの時点で DoWork デバッガーが中断します。 複数BackgroundWorkerの がある場合は、イベント ハンドラーを の特定のインスタンスBackgroundWorkerに結合DoWorkするため、それらのいずれも直接参照しないでください。 代わりに、イベント ハンドラーで パラメーターをキャストして、 sender にアクセスBackgroundWorkerするDoWork必要があります。

イベント ハンドラーでユーザー インターフェイス オブジェクトを操作しないように注意する DoWork 必要があります。 代わりに、BackgroundWorker のイベントを通じてユーザー インターフェイスと通信します。

イベントを処理する方法の詳細については、次を参照してください。処理とイベントの発生します。

適用対象

こちらもご覧ください