チュートリアル: 操作をバックグラウンドで実行するWalkthrough: Running an Operation in the Background

完了に長い時間がかかる操作を実行しており、ユーザー インターフェイスで遅延が発生しないようにするにはBackgroundWorker クラスを使用して別のスレッドで操作を実行できます。If you have an operation that will take a long time to complete, and you do not want to cause delays in your user interface, you can use the BackgroundWorker class to run the operation on another thread.

この例で使用するコードの完全な一覧については、次を参照してください。方法。バックグラウンドで操作を実行する」を参照してください。For a complete listing of the code used in this example, see How to: Run an Operation in the Background.

バック グラウンドで操作を実行します。Run an operation in the background

  1. 2 つの Visual Studio での Windows フォーム デザイナーでアクティブなフォームにドラッグButtonコントロールをツールボックスフォームを設定して、NameTextに従って、ボタンのプロパティ、次の表にします。With your form active in the Windows Forms Designer in Visual Studio, drag two Button controls from the Toolbox to the form, and then set the Name and Text properties of the buttons according to the following table.

    ボタンButton 名前Name テキストText
    button1 startBtn [開始]Start
    button2 cancelBtn キャンセルCancel
  2. 開く、ツールボックス、クリックして、コンポーネントタブをクリックし、ドラッグ、BackgroundWorkerコンポーネントをフォームにします。Open the Toolbox, click the Components tab, and then drag the BackgroundWorker component onto your form.

    backgroundWorker1コンポーネントに表示されます、コンポーネント トレイします。The backgroundWorker1 component appears in the Component Tray.

  3. [プロパティ] ウィンドウで、 WorkerSupportsCancellation プロパティを trueに設定します。In the Properties window, set the WorkerSupportsCancellation property to true.

  4. プロパティウィンドウのイベントボタンをクリックし、ダブルクリック、DoWorkRunWorkerCompletedイベントをイベント ハンドラーを作成します。In the Properties window, click on the Events button, and then double-click the DoWork and RunWorkerCompleted events to create event handlers.

  5. 挿入に時間のかかるコード、DoWorkイベント ハンドラー。Insert your time-consuming code into the DoWork event handler.

  6. 操作に必要なすべてのパラメーターを抽出、Argumentのプロパティ、DoWorkEventArgsパラメーター。Extract any parameters required by the operation from the Argument property of the DoWorkEventArgs parameter.

  7. 計算の結果に割り当てる、Resultのプロパティ、DoWorkEventArgsします。Assign the result of the computation to the Result property of the DoWorkEventArgs.

    これは使用できる、RunWorkerCompletedイベント ハンドラー。This is will be available to the RunWorkerCompleted event handler.

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        // Do not access the form's BackgroundWorker reference directly.
        // Instead, use the reference provided by the sender parameter.
        BackgroundWorker bw = sender as BackgroundWorker;
    
        // Extract the argument.
        int arg = (int)e.Argument;
    
        // Start the time-consuming operation.
        e.Result = TimeConsumingOperation(bw, arg);
    
        // If the operation was canceled by the user, 
        // set the DoWorkEventArgs.Cancel property to true.
        if (bw.CancellationPending)
        {
            e.Cancel = true;
        }
    }
    
    Private Sub backgroundWorker1_DoWork( _
    sender As Object, e As DoWorkEventArgs) _
    Handles backgroundWorker1.DoWork
    
       ' Do not access the form's BackgroundWorker reference directly.
       ' Instead, use the reference provided by the sender parameter.
       Dim bw As BackgroundWorker = CType( sender, BackgroundWorker )
       
       ' Extract the argument.
       Dim arg As Integer = Fix(e.Argument)
       
       ' Start the time-consuming operation.
       e.Result = TimeConsumingOperation(bw, arg)
       
       ' If the operation was canceled by the user, 
       ' set the DoWorkEventArgs.Cancel property to true.
       If bw.CancellationPending Then
          e.Cancel = True
       End If
    
    End Sub   
    
  8. 操作の結果を取得するためのコードを挿入、RunWorkerCompletedイベント ハンドラー。Insert code for retrieving the result of your operation in the RunWorkerCompleted event handler.

    // This event handler demonstrates how to interpret 
    // the outcome of the asynchronous operation implemented
    // in the DoWork event handler.
    private void backgroundWorker1_RunWorkerCompleted(
        object sender, 
        RunWorkerCompletedEventArgs e)
    {   
        if (e.Cancelled)
        {
            // The user canceled the operation.
            MessageBox.Show("Operation was canceled");
        }
        else if (e.Error != null)
        {
            // There was an error during the operation.
            string msg = String.Format("An error occurred: {0}", e.Error.Message);
            MessageBox.Show(msg);
        }
        else
        {
            // The operation completed normally.
            string msg = String.Format("Result = {0}", e.Result);
            MessageBox.Show(msg);
        }
    }
    
    ' This event handler demonstrates how to interpret 
    ' the outcome of the asynchronous operation implemented
    ' in the DoWork event handler.
    Private Sub backgroundWorker1_RunWorkerCompleted( _
    sender As Object, e As RunWorkerCompletedEventArgs) _
    Handles backgroundWorker1.RunWorkerCompleted
    
       If e.Cancelled Then
          ' The user canceled the operation.
          MessageBox.Show("Operation was canceled")
       ElseIf (e.Error IsNot Nothing) Then
          ' There was an error during the operation.
          Dim msg As String = String.Format("An error occurred: {0}", e.Error.Message)
          MessageBox.Show(msg)
       Else
          ' The operation completed normally.
          Dim msg As String = String.Format("Result = {0}", e.Result)
          MessageBox.Show(msg)
       End If
    End Sub   
    
  9. TimeConsumingOperation メソッドを実装します。Implement the TimeConsumingOperation method.

    // This method models an operation that may take a long time 
    // to run. It can be cancelled, it can raise an exception,
    // or it can exit normally and return a result. These outcomes
    // are chosen randomly.
    private int TimeConsumingOperation( 
        BackgroundWorker bw, 
        int sleepPeriod )
    {
        int result = 0;
    
        Random rand = new Random();
    
        while (!bw.CancellationPending)
        {
            bool exit = false;
    
            switch (rand.Next(3))
            {
                // Raise an exception.
                case 0:
                {
                    throw new Exception("An error condition occurred.");
                    break;
                }
    
                // Sleep for the number of milliseconds
                // specified by the sleepPeriod parameter.
                case 1:
                {
                    Thread.Sleep(sleepPeriod);
                    break;
                }
    
                // Exit and return normally.
                case 2:
                {
                    result = 23;
                    exit = true;
                    break;
                }
    
                default:
                {
                    break;
                }
            }
    
            if( exit )
            {
                break;
            }
        }
    
        return result;
    }
    
    ' This method models an operation that may take a long time 
    ' to run. It can be cancelled, it can raise an exception,
    ' or it can exit normally and return a result. These outcomes
    ' are chosen randomly.
    Private Function TimeConsumingOperation( _
    bw As BackgroundWorker, _
    sleepPeriod As Integer) As Integer
    
       Dim result As Integer = 0
       
       Dim rand As New Random()
       
         While Not bw.CancellationPending
             Dim [exit] As Boolean = False
    
             Select Case rand.Next(3)
                 ' Raise an exception.
                 Case 0
                     Throw New Exception("An error condition occurred.")
                     Exit While
    
                     ' Sleep for the number of milliseconds
                     ' specified by the sleepPeriod parameter.
                 Case 1
                     Thread.Sleep(sleepPeriod)
                     Exit While
    
                     ' Exit and return normally.
                 Case 2
                     result = 23
                     [exit] = True
                     Exit While
    
                 Case Else
                     Exit While
             End Select
    
             If [exit] Then
                 Exit While
             End If
         End While
       
       Return result
    End Function
    
  10. Windows フォーム デザイナーで、ダブルクリックstartButtonを作成する、Clickイベント ハンドラー。In the Windows Forms Designer, double-click startButton to create the Click event handler.

  11. 呼び出す、RunWorkerAsyncメソッドで、Clickのイベント ハンドラーstartButtonします。Call the RunWorkerAsync method in the Click event handler for startButton.

    private void startBtn_Click(object sender, EventArgs e)
    {
        this.backgroundWorker1.RunWorkerAsync(2000);
    }
    
    Private Sub startButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles startBtn.Click
        Me.backgroundWorker1.RunWorkerAsync(2000)
    End Sub
    
  12. Windows フォーム デザイナーで、ダブルクリックcancelButtonを作成する、Clickイベント ハンドラー。In the Windows Forms Designer, double-click cancelButton to create the Click event handler.

  13. 呼び出す、CancelAsyncメソッドで、Clickのイベント ハンドラーcancelButtonします。Call the CancelAsync method in the Click event handler for cancelButton.

    private void cancelBtn_Click(object sender, EventArgs e)
    {
        this.backgroundWorker1.CancelAsync();
    }
    
    Private Sub cancelButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cancelBtn.Click
        Me.backgroundWorker1.CancelAsync()
    End Sub
    
  14. ファイルの上部にある、System.ComponentModel および System.Threading 名前空間をインポートします。At the top of the file, import the System.ComponentModel and System.Threading namespaces.

    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Threading;
    using System.Windows.Forms;
    
    Imports System.ComponentModel
    Imports System.Drawing
    Imports System.Threading
    Imports System.Windows.Forms
    
  15. キーを押してF6ソリューションをビルドし、キーを押しますCtrl+f5 キーを押してデバッガーの外部のアプリケーションを実行します。Press F6 to build the solution, and then press Ctrl+F5 to run the application outside the debugger.

    注意

    キーを押す場合F5で、デバッガーの下でアプリケーションを実行する、例外が発生した、TimeConsumingOperationメソッドがキャッチされ、デバッガーによって表示されます。If you press F5 to run the application under the debugger, the exception raised in the TimeConsumingOperation method is caught and displayed by the debugger. デバッガーの外部のアプリケーションを実行すると、 BackgroundWorker 、例外を処理し、キャッシュで、Errorのプロパティ、RunWorkerCompletedEventArgsします。When you run the application outside the debugger, the BackgroundWorker handles the exception and caches it in the Error property of the RunWorkerCompletedEventArgs.

  16. をクリックして、開始、非同期操作を実行するボタンをクリックし、をクリックし、キャンセル実行中の非同期操作を停止するボタンをクリックします。Click the Start button to run an asynchronous operation, and then click the Cancel button to stop a running asynchronous operation.

    各操作の結果は、MessageBox に表示されます。The outcome of each operation is displayed in a MessageBox.

次の手順Next steps

関連項目See also