BackgroundWorker.DoWork 事件

定义

调用 RunWorkerAsync() 时发生。

public:
 event System::ComponentModel::DoWorkEventHandler ^ DoWork;
public event System.ComponentModel.DoWorkEventHandler DoWork;
public event System.ComponentModel.DoWorkEventHandler? DoWork;
member this.DoWork : System.ComponentModel.DoWorkEventHandler 
Public Custom Event DoWork As DoWorkEventHandler 

事件类型

示例

下面的代码示例演示如何使用 DoWork 事件启动异步操作。 此代码示例是为 BackgroundWorker 类提供的一个更大示例的一部分。

// 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 = 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 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

注解

调用 RunWorkerAsync 方法时会引发此事件。 在这里,可以启动执行可能耗时的工作的操作。

事件处理程序中的DoWork代码应定期检查CancellationPending属性值,如果true为 ,则中止操作。 发生这种情况时,可以将 的标志设置为 Canceltrue,事件处理程序Cancelled中的 RunWorkerCompletedSystem.ComponentModel.RunWorkerCompletedEventArgs标志将设置为 trueSystem.ComponentModel.DoWorkEventArgs

注意

请注意,事件处理程序中的 DoWork 代码可能会在发出取消请求时完成其工作,并且轮询循环可能会错过 CancellationPending 设置为 true。 在这种情况下,即使发出了取消请求,Cancelled事件处理程序中的 RunWorkerCompleted 的标志System.ComponentModel.RunWorkerCompletedEventArgs也不会设置为 true。 这种情况称为 争用条件 ,是多线程编程中常见的问题。 有关多线程设计问题的详细信息,请参阅 托管线程处理最佳做法

如果操作生成结果,则可以将结果分配给 DoWorkEventArgs.Result 属性。 这将对 RunWorkerCompleted 属性中的 RunWorkerCompletedEventArgs.Result 事件处理程序可用。

如果操作引发代码未处理的异常,则会 BackgroundWorker 捕获异常并将其传递到 RunWorkerCompleted 事件处理程序中,其中它作为 ErrorSystem.ComponentModel.RunWorkerCompletedEventArgs属性公开。 如果在 Visual Studio 调试器下运行,则调试器将在引发未经处理的异常的 DoWork 事件处理程序中中断。 如果有多个 BackgroundWorker,则不应直接引用其中的任何一个,因为这会将 DoWork 事件处理程序与 的特定实例 BackgroundWorker相耦合。 相反,应通过在事件处理程序中DoWork强制转换 sender 参数来访问 BackgroundWorker

必须小心不要在事件处理程序中 DoWork 操作任何用户界面对象。 而应该通过 BackgroundWorker 事件与用户界面进行通信。

有关如何处理事件的详细信息,请参阅 处理和引发事件

适用于

另请参阅