BackgroundWorker.RunWorkerAsync 方法

定义

开始执行后台操作。

重载

RunWorkerAsync()

开始执行后台操作。

RunWorkerAsync(Object)

开始执行后台操作。

RunWorkerAsync()

Source:
BackgroundWorker.cs
Source:
BackgroundWorker.cs
Source:
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)

Source:
BackgroundWorker.cs
Source:
BackgroundWorker.cs
Source:
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

另请参阅

适用于