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 ()

例外狀況

範例

下列程式代碼範例示範如何使用 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 事件處理常式中,執行背景作業時使用的參數。

例外狀況

範例

下列程式代碼範例示範如何使用 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 就會引發 事件,進而開始執行您的背景作業。

如果您的工作需要參數,您可以將它當做 argument 參數提供給 RunWorkerAsync

如果背景作業已在執行中,再次呼叫 RunWorkerAsync 將會引發 InvalidOperationException

另請參閱

適用於