BackgroundWorker.RunWorkerAsync 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
開始執行背景操作。
多載
| 名稱 | Description |
|---|---|
| RunWorkerAsync() |
開始執行背景操作。 |
| RunWorkerAsync(Object) |
開始執行背景操作。 |
RunWorkerAsync()
開始執行背景操作。
public:
void RunWorkerAsync();
public void RunWorkerAsync();
member this.RunWorkerAsync : unit -> unit
Public Sub RunWorkerAsync ()
例外狀況
IsBusy 為 true。
範例
以下程式碼範例示範 RunWorkerAsync 使用此方法啟動非同步操作。 它是《 如何:在背景下載檔案》中描述的一個更大範例的一部分。
void downloadButton_Click(object sender, EventArgs e)
{
// Start the download operation in the background.
backgroundWorker1.RunWorkerAsync();
// Disable the button for the duration of the download.
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 (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)
開始執行背景操作。
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)
參數
例外狀況
IsBusy 為 true。
範例
以下程式碼範例示範 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。