通过


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.
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)。 发生此情况时,可以将标志trueSystem.ComponentModel.DoWorkEventArgs设置为 Cancel ,事件处理程序Cancelled中的RunWorkerCompleted标志System.ComponentModel.RunWorkerCompletedEventArgs将设置为 true

注意

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

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

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

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

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

适用于

另请参阅