BackgroundWorker.DoWork Event

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Occurs when RunWorkerAsync is called.

Namespace:  System.ComponentModel
Assembly:  System (in System.dll)

Syntax

'Declaration
Public Event DoWork As DoWorkEventHandler
public event DoWorkEventHandler DoWork

Remarks

This event is raised when you call the RunWorkerAsync method. This is where you start the time-consuming operation.

Your code in the DoWork event handler should periodically check the CancellationPending property value and stop the operation if it is true. When this occurs, you set the Cancel property of System.ComponentModel.DoWorkEventArgs to true, and the Cancelled property of System.ComponentModel.RunWorkerCompletedEventArgs in your RunWorkerCompleted event handler will be set to true.

Caution noteCaution:

Be aware that your code in the DoWork event handler may complete its work as a cancellation request is being made, and your polling loop may miss CancellationPending being set to true. In this case, the Cancelled flag of System.ComponentModel.RunWorkerCompletedEventArgs in your RunWorkerCompleted event handler will not be set to true, even though a cancellation request was made. This situation is called a race condition and is a common concern in multithreaded programming. For more information about multithreading design issues, see Best Practices for Managed Threading.

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 have more than one BackgroundWorker, you should not reference 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.

Examples

The following code example demonstrates the use of the DoWork event to start a background operation. To view the complete code for this sample, see How to: Use a Background Worker.

Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
    Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)

    For i = 1 To 10
        If bw.CancellationPending = True Then
            e.Cancel = True
            Exit For
        Else
            ' Perform a time consuming operation and report progress.
            System.Threading.Thread.Sleep(500)
            bw.ReportProgress(i * 10)
        End If
    Next
End Sub
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker;

    for (int i = 1; (i <= 10); i++)
    {
        if ((worker.CancellationPending == true))
        {
            e.Cancel = true;
            break;
        }
        else
        {
            // Perform a time consuming operation and report progress.
            System.Threading.Thread.Sleep(500);
            worker.ReportProgress((i * 10));
        }
    }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.