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

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

このイベントは、RunWorkerAsync メソッドを呼び出したときに発生します。これは、時間を要する可能性のある処理を実行する操作を開始するイベントです。
DoWork イベント ハンドラ内のコードでは、CancellationPending プロパティ値を定期的にチェックし、このプロパティが true の場合は操作を中止する必要があります。これが発生すると、System.ComponentModel.DoWorkEventArgs の Cancel フラグを true に設定できます。また、RunWorkerCompleted イベント ハンドラの System.ComponentModel.RunWorkerCompletedEventArgs の Cancelled フラグが true に設定されます。
![]() |
---|
DoWork イベント ハンドラ内のコードは、キャンセル要求が行われているときに処理を終了できること、また、ポーリング ループは、CancellationPending に true を設定し損ねる場合があることに注意してください。この場合、キャンセル要求が行われていても、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 のイベントを通じてユーザー インターフェイスと通信します。

DoWork イベントを使用して非同期操作を開始するコード例を次に示します。このコード例は、BackgroundWorker クラスのトピックで取り上げているコード例の一部分です。
' 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 'backgroundWorker1_DoWork
// 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, // 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 = (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.set_Result(new Long(ComputeFibonacci(System.Convert.ToInt32 (e.get_Argument()), worker, e))); //e.Result = ComputeFibonacci((int)e.Argument, worker, e); } //backgroundWorker1_DoWork

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.DoWork イベントを検索する場合は、下記のリンクをクリックしてください。

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