Namespace:
System.ComponentModel
Assembly:
System (in System.dll)
Visual Basic (Declaration)
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);
}
JScript does not support events.
This event is raised when you call the RunWorkerAsync method. This is where you start the operation that performs the potentially time-consuming work.
Your code in the DoWork event handler should periodically check the CancellationPending property value and abort the operation if it is true. When this occurs, you can set the Cancel flag of System.ComponentModel..::.DoWorkEventArgs to true, and the Cancelled flag of System.ComponentModel..::.RunWorkerCompletedEventArgs in your RunWorkerCompleted event handler will be set to true.
If your operation produces a result, you can assign the result to the DoWorkEventArgs..::.Result property. This will be available to the RunWorkerCompleted event handler in the RunWorkerCompletedEventArgs..::.Result property.
If the operation raises an exception that your code does not handle, the BackgroundWorker catches the exception and passes it into the RunWorkerCompleted event handler, where it is exposed as the Error property of System.ComponentModel..::.RunWorkerCompletedEventArgs. If you are running under the Visual Studio debugger, the debugger will break at the point in the DoWork event handler where the unhandled exception was raised. If you have more than one BackgroundWorker, you should not reference any of them directly, as this would couple your DoWork event handler to a specific instance of BackgroundWorker. Instead, you should access your BackgroundWorker by casting the sender parameter in your DoWork event handler.
You must be careful not to manipulate any user-interface objects in your DoWork event handler. Instead, communicate to the user interface through the BackgroundWorker events.
For more information about handling events, see Consuming Events.
The following code example demonstrates the use of the DoWork event to start an asynchronous operation. This code example is part of a larger example provided for the BackgroundWorker class.
' 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 );
}
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0
Reference
Other Resources