BackgroundWorker.DoWork イベントとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > BackgroundWorker.DoWork イベントの意味・解説 

BackgroundWorker.DoWork イベント

メモ : このイベントは、.NET Framework version 2.0新しく追加されたものです。

RunWorkerAsync が呼び出されたときに発生します

名前空間: System.ComponentModel
アセンブリ: System (system.dll 内)
構文構文

Public Event DoWork As DoWorkEventHandler
Dim instance As BackgroundWorker
Dim handler As DoWorkEventHandler

AddHandler instance.DoWork, handler
public event DoWorkEventHandler DoWork
public:
event DoWorkEventHandler^ DoWork {
    void add (DoWorkEventHandler^ value);
    void remove (DoWorkEventHandler^ value);
}
/** @event */
public void add_DoWork (DoWorkEventHandler
 value)

/** @event */
public void remove_DoWork (DoWorkEventHandler
 value)
JScript では、イベント使用できますが、新規に宣言することはできません。
解説解説

このイベントは、RunWorkerAsync メソッド呼び出したときに発生します。これは、時間要する可能性のある処理を実行する操作開始するイベントです。

DoWork イベント ハンドラ内のコードでは、CancellationPending プロパティ値を定期的にチェックし、このプロパティtrue場合操作中止する必要があります。これが発生すると、System.ComponentModel.DoWorkEventArgs の Cancel フラグtrue設定できますまた、RunWorkerCompleted イベント ハンドラの System.ComponentModel.RunWorkerCompletedEventArgs の Cancelled フラグtrue設定されます。

注意に関するメモ注意

DoWork イベント ハンドラ内のコードは、キャンセル要求が行われているときに処理を終了できることまた、ポーリング ループは、CancellationPendingtrue設定し損ねる場合があることに注意してください。この場合キャンセル要求が行われていても、RunWorkerCompleted イベント ハンドラSystem.ComponentModel.RunWorkerCompletedEventArgsCancelled フラグtrue設定されません。この状況競合状態呼ばれマルチスレッドプログラミングにおける一般的な懸念事項です。マルチスレッド デザインに関する問題詳細については、「マネージ スレッド処理の実施」を参照してください

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

コード処理されない例外操作発生した場合、BackgroundWorker はその例外キャッチしRunWorkerCompleted イベント ハンドラ渡します。このイベント ハンドラでは、System.ComponentModel.RunWorkerCompletedEventArgsError プロパティとして例外公開されます。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
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

BackgroundWorker.DoWork イベントのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



BackgroundWorker.DoWork イベントのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS