更新:2007 年 11 月
命名空间:
System.ComponentModel 程序集:
System(在 System.dll 中)
Public Event DoWork As DoWorkEventHandler
Dim instance As BackgroundWorker
Dim handler As DoWorkEventHandler
AddHandler instance.DoWork, handler
public event DoWorkEventHandler DoWork
public:
event DoWorkEventHandler^ DoWork {
void add (DoWorkEventHandler^ value);
void remove (DoWorkEventHandler^ value);
}
/** @event */
public void add_DoWork (DoWorkEventHandler value)
/** @event */
public void remove_DoWork (DoWorkEventHandler value)
调用 RunWorkerAsync 方法时将引发此事件。在此,您就可以启动操作来执行可能很耗时的工作。
DoWork 事件处理程序中的代码应定期检查 CancellationPending 属性值,并在该值为 true 时中止操作。出现这种情况时,可以将 System.ComponentModel..::.DoWorkEventArgs 的 Cancel 标志设置为 true,同时将 RunWorkerCompleted 事件处理程序中的 System.ComponentModel..::.RunWorkerCompletedEventArgs 的 Cancelled 标志设置为 true。
如果您的操作产生了结果,则可以将结果分配给 DoWorkEventArgs..::.Result 属性。这将可以用于 RunWorkerCompletedEventArgs..::.Result 属性中的 RunWorkerCompleted 事件处理程序。
如果该操作引发您的代码无法处理的异常,则 BackgroundWorker 将捕获该异常并将其传递到 RunWorkerCompleted 事件处理程序,在该事件处理程序中,该异常作为 System.ComponentModel..::.RunWorkerCompletedEventArgs 的 Error 属性公开。如果您在 Visual Studio 调试器下运行,则该调试器将在 DoWork 事件处理程序中引发未处理异常的位置中断。如果您有多个 BackgroundWorker,则不应当直接引用其中的任何一个,因为这会将 DoWork 事件处理程序耦合到 BackgroundWorker 的特定实例。而是应当通过在 DoWork 事件处理程序中强制转换 sender 参数来访问 BackgroundWorker。
您必须非常小心,确保在 DoWork 事件处理程序中不操作任何用户界面对象。而应该通过 BackgroundWorker 事件与用户界面进行通信。
有关处理事件的更多信息,请参见 使用事件。
下面的代码示例演示如何使用 DoWork 事件启动异步操作。此代码示例摘自一个为 BackgroundWorker 类提供的更大的示例。
' 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
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
参考
其他资源