BackgroundWorker.RunWorkerAsync メソッド

定義

バックグラウンド操作の実行を開始します。

オーバーロード

RunWorkerAsync()

バックグラウンド操作の実行を開始します。

RunWorkerAsync(Object)

バックグラウンド操作の実行を開始します。

RunWorkerAsync()

ソース:
BackgroundWorker.cs
ソース:
BackgroundWorker.cs
ソース:
BackgroundWorker.cs

バックグラウンド操作の実行を開始します。

public:
 void RunWorkerAsync();
public void RunWorkerAsync ();
member this.RunWorkerAsync : unit -> unit
Public Sub RunWorkerAsync ()

例外

IsBusytrueです。

次のコード例では、 メソッドを使用して RunWorkerAsync 非同期操作を開始する方法を示します。 これは、「 方法: バックグラウンドでファイルをダウンロードする」で説明されているより大きな例の一部です。

private void downloadButton_Click(object sender, EventArgs e)
{
    // Start the download operation in the background.
    this.backgroundWorker1.RunWorkerAsync();

    // Disable the button for the duration of the download.
    this.downloadButton.Enabled = false;

    // Once you have started the background thread you 
    // can exit the handler and the application will 
    // wait until the RunWorkerCompleted event is raised.

    // Or if you want to do something else in the main thread,
    // such as update a progress bar, you can do so in a loop 
    // while checking IsBusy to see if the background task is
    // still running.

    while (this.backgroundWorker1.IsBusy)
    {
        progressBar1.Increment(1);
        // Keep UI messages moving, so the form remains 
        // responsive during the asynchronous operation.
        Application.DoEvents();
    }
}
Private Sub downloadButton_Click( _
    ByVal sender As Object, _
    ByVal e As EventArgs) _
    Handles downloadButton.Click

    ' Start the download operation in the background.
    Me.backgroundWorker1.RunWorkerAsync()

    ' Disable the button for the duration of the download.
    Me.downloadButton.Enabled = False

    ' Once you have started the background thread you 
    ' can exit the handler and the application will 
    ' wait until the RunWorkerCompleted event is raised.

    ' If you want to do something else in the main thread,
    ' such as update a progress bar, you can do so in a loop 
    ' while checking IsBusy to see if the background task is
    ' still running.
    While Me.backgroundWorker1.IsBusy
        progressBar1.Increment(1)
        ' Keep UI messages moving, so the form remains 
        ' responsive during the asynchronous operation.
        Application.DoEvents()
    End While
End Sub

注釈

メソッドは RunWorkerAsync 、非同期的に実行されている操作を開始する要求を送信します。 要求が処理されると、 DoWork イベントが発生し、バックグラウンド操作の実行が開始されます。

バックグラウンド操作が既に実行されている場合は、もう一度 を呼び出すと RunWorkerAsync 、 が発生します InvalidOperationException

こちらもご覧ください

適用対象

RunWorkerAsync(Object)

ソース:
BackgroundWorker.cs
ソース:
BackgroundWorker.cs
ソース:
BackgroundWorker.cs

バックグラウンド操作の実行を開始します。

public:
 void RunWorkerAsync(System::Object ^ argument);
public void RunWorkerAsync (object argument);
public void RunWorkerAsync (object? argument);
member this.RunWorkerAsync : obj -> unit
Public Sub RunWorkerAsync (argument As Object)

パラメーター

argument
Object

DoWork イベント ハンドラーで実行されるバックグラウンド操作で使用するパラメーター。

例外

IsBusytrueです。

次のコード例では、 メソッドを使用して RunWorkerAsync 非同期操作を開始する方法を示します。 このコード例は、BackgroundWorker クラスのために提供されている大規模な例の一部です。

// Start the asynchronous operation.
backgroundWorker1->RunWorkerAsync( numberToCompute );
// Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync(numberToCompute);

' Start the asynchronous operation.
backgroundWorker1.RunWorkerAsync(numberToCompute)

注釈

メソッドは RunWorkerAsync 、非同期的に実行されている操作を開始する要求を送信します。 要求が処理されると、 DoWork イベントが発生し、バックグラウンド操作の実行が開始されます。

操作に パラメーターが必要な場合は、 に パラメーターRunWorkerAsyncとしてargument指定できます。

バックグラウンド操作が既に実行されている場合は、もう一度 を呼び出すと RunWorkerAsync 、 が発生します InvalidOperationException

こちらもご覧ください

適用対象