请单击以进行评分并提供反馈
MSDN
MSDN Library
.NET 开发
.NET Framework
 DoWork 事件

  开启低带宽视图
此页面仅适用于
Microsoft Visual Studio 2008/.NET Framework 3.5

同时提供下列产品的其他版本:
.NET Framework 类库
BackgroundWorker..::.DoWork 事件

更新:2007 年 11 月

调用 RunWorkerAsync 时发生。

命名空间:  System.ComponentModel
程序集:  System(在 System.dll 中)

Visual Basic(声明)
Public Event DoWork As DoWorkEventHandler
Visual Basic (用法)
Dim instance As BackgroundWorker
Dim handler As DoWorkEventHandler

AddHandler instance.DoWork, handler
C#
public event DoWorkEventHandler DoWork
Visual C++
public:
 event DoWorkEventHandler^ DoWork {
    void add (DoWorkEventHandler^ value);
    void remove (DoWorkEventHandler^ value);
}
J#
/** @event */
public void add_DoWork (DoWorkEventHandler value)
/** @event */
public void remove_DoWork (DoWorkEventHandler value)
JScript
JScript 不支持事件。

调用 RunWorkerAsync 方法时将引发此事件。在此,您就可以启动操作来执行可能很耗时的工作。

DoWork 事件处理程序中的代码应定期检查 CancellationPending 属性值,并在该值为 true 时中止操作。出现这种情况时,可以将 System.ComponentModel..::.DoWorkEventArgsCancel 标志设置为 true,同时将 RunWorkerCompleted 事件处理程序中的 System.ComponentModel..::.RunWorkerCompletedEventArgsCancelled 标志设置为 true

警告:

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

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

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

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

有关处理事件的更多信息,请参见 使用事件

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

Visual Basic
' 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

C#
// 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);
}

Visual C++
// 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 );
}

J#
// 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

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求

.NET Framework

受以下版本支持:3.5、3.0、2.0
社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
© 2009 Microsoft Corporation 版权所有。 保留所有权利  |  商标  |  隐私权声明
Page view tracker