BackgroundWorker 元件概觀
更新:2007 年 11 月
有很多常見的執行的作業要花很長時間執行。例如:
影像下載
Web 服務引動過程
檔案下載與上載 (包含在點對點應用程式中)
複雜的本機計算
資料庫交易
本機磁碟存取 (相對於記憶體存取,速度較慢)
類似這些的作業,可能造成使用者介面在執行時無回應。如果您面臨與此類作業關聯的冗長延遲,而想要回應迅速的 UI,BackgroundWorker 元件可提供方便的方案。
BackgroundWorker 元件會提供您在不同於應用程式主要 UI 執行緒的執行緒上,非同步 (「在幕後執行」) 執行耗時作業的能力。若要使用 BackgroundWorker,您只要告訴它要在幕後執行哪種耗時的背景工作方法,然後呼叫 RunWorkerAsync 方法。對執行緒的呼叫會繼續正常執行,直到工作方法非同步執行。當方法完成時,BackgroundWorker 會藉由引發 RunWorkerCompleted 事件來警示呼叫執行緒,此事件會選擇性地包含作業的結果。
可以從 [工具箱] 的 [元件] 索引標籤中取用 BackgroundWorker 元件。若要在表單中加入 BackgroundWorker,請將 BackgroundWorker 元件拖曳至表單上。它會顯示在元件匣中,而其屬性則顯示於 [屬性] 視窗。
若要啟動同步作業,請使用 RunWorkerAsync 方法。RunWorkerAsync 使用選擇性的 object 參數,此參數可用來傳遞引數至背景工作方法。BackgroundWorker 類別會公開 (Expose) DoWork 事件,您的背景工作執行緒 (Worker Thread) 會透過 DoWork 事件處理常式附加至這個事件。
DoWork 事件處理常式會使用 DoWorkEventArgs 參數,此參數具有 Argument 屬性。這個參數接收來自 RunWorkerAsync 的參數,並且可以傳遞至您的背景工作方法,在 DoWork 事件處理常式中則會呼叫此背景工作方法。下列範例示範如何指派得自名為 ComputeFibonacci 的背景工作方法的結果。這是 HOW TO:實作使用背景作業的表單中完整範例的一部分。
' This event handler is where the actual work is done.
Private Sub backgroundWorker1_DoWork( _
ByVal sender As Object, _
ByVal e As DoWorkEventArgs) _
Handles backgroundWorker1.DoWork
' Get the BackgroundWorker object that raised this event.
Dim worker As BackgroundWorker = _
CType(sender, BackgroundWorker)
' Assign the result of the computation
' to the Result property of the DoWorkEventArgs
' object. This is will be available to the
' RunWorkerCompleted eventhandler.
e.Result = ComputeFibonacci(e.Argument, worker, e)
End Sub 'backgroundWorker1_DoWork
// This event handler is where the actual,
// potentially time-consuming work is done.
private void backgroundWorker1_DoWork(object sender,
DoWorkEventArgs e)
{
// Get the BackgroundWorker that raised this event.
BackgroundWorker worker = sender as BackgroundWorker;
// Assign the result of the computation
// to the Result property of the DoWorkEventArgs
// object. This is will be available to the
// RunWorkerCompleted eventhandler.
e.Result = ComputeFibonacci((int)e.Argument, worker, e);
}
// This event handler is where the actual,
// potentially time-consuming work is done.
void backgroundWorker1_DoWork( Object^ sender, DoWorkEventArgs^ e )
{
// Get the BackgroundWorker that raised this event.
BackgroundWorker^ worker = dynamic_cast<BackgroundWorker^>(sender);
// Assign the result of the computation
// to the Result property of the DoWorkEventArgs
// object. This is will be available to the
// RunWorkerCompleted eventhandler.
e->Result = ComputeFibonacci( safe_cast<Int32>(e->Argument), worker, e );
}
// This event handler is where the actual,
// potentially time-consuming work is done.
private void backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e)
{
// Get the BackgroundWorker that raised this event.
BackgroundWorker worker = (BackgroundWorker)sender;
// Assign the result of the computation
// to the Result property of the DoWorkEventArgs
// object. This is will be available to the
// RunWorkerCompleted eventhandler.
e.set_Result(new Long(ComputeFibonacci(System.Convert.ToInt32
(e.get_Argument()), worker, e)));
//e.Result = ComputeFibonacci((int)e.Argument, worker, e);
} //backgroundWorker1_DoWork
如需使用事件處理常式的詳細資訊,請參閱 事件和委派。
![]() |
---|
在使用任何類型的多執行緒時,很可能會接觸到極嚴重且複雜的錯誤。在實作任何使用多執行緒處理的方案之前,請參閱 Managed 執行緒處理的最佳實施方針。 |
如需使用 BackgroundWorker 類別的詳細資訊,請參閱 HOW TO:在背景執行作業。