次の方法で共有


BackgroundWorker コンポーネントの概要

一般的に実行される操作の多くは、実行に時間がかかる可能性があります。 例えば:

  • イメージのダウンロード

  • Web サービスの呼び出し

  • ファイルのダウンロードとアップロード (ピアツーピア アプリケーションを含む)

  • 複雑なローカル計算

  • データベース トランザクション

  • ローカルディスクへのアクセスは、メモリアクセスに比べて速度が遅い場合があります。

このような操作により、実行中にユーザー インターフェイスがブロックされる可能性があります。 応答性の高い UI が必要で、このような操作に関連する長い遅延が発生した場合、BackgroundWorker コンポーネントは便利なソリューションを提供します。

BackgroundWorker コンポーネントを使用すると、アプリケーションのメイン UI スレッドとは異なるスレッドで、時間のかかる操作を非同期的に ("バックグラウンドで") 実行できます。 BackgroundWorkerを使用するには、バックグラウンドで実行する時間のかかる worker メソッドを指定し、RunWorkerAsync メソッドを呼び出します。 ワーカー メソッドが非同期的に実行されている間、呼び出し元のスレッドは引き続き正常に実行されます。 メソッドが完了すると、BackgroundWorker は、必要に応じて操作の結果を含む RunWorkerCompleted イベントを発生させて呼び出し元のスレッドに警告します。

BackgroundWorker コンポーネントは、[コンポーネント] タブの [ツールボックス]から使用できます。フォームに BackgroundWorker を追加するには、BackgroundWorker コンポーネントをフォームにドラッグします。 コンポーネント トレイに表示され、そのプロパティが プロパティ ウィンドウに表示されます。

非同期操作を開始するには、RunWorkerAsync メソッドを使用します。 RunWorkerAsync は省略可能な object パラメーターを受け取ります。これは、引数をワーカー メソッドに渡すために使用できます。 BackgroundWorker クラスは、ワーカー スレッドが DoWork イベント ハンドラーを介してアタッチされる DoWork イベントを公開します。

DoWork イベント ハンドラーは、Argument プロパティを持つ DoWorkEventArgs パラメーターを受け取ります。 このプロパティは、RunWorkerAsync からパラメーターを受け取り、DoWork イベント ハンドラーで呼び出される worker メソッドに渡すことができます。 次の例は、ComputeFibonacciという worker メソッドから結果を割り当てる方法を示しています。 これは大きな例の一部であり、「方法: バックグラウンド操作を使用するフォームを実装する」で確認できます。

// 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

イベント ハンドラーの使用方法の詳細については、「イベント」を参照してください。

注意

あらゆる種類のマルチスレッドを使用する場合、非常に深刻で複雑なバグに自分自身を公開する可能性があります。 マルチスレッドを使用するソリューションを実装する前に、マネージド スレッドのベスト プラクティス を参照してください。

BackgroundWorker クラスの使用方法の詳細については、「方法: バックグラウンドで操作を実行する」を参照してください。

関連項目