DoWork Event
Collapse the table of content
Expand the table of content

BackgroundWorker.DoWork Event

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Occurs when RunWorkerAsync is called.

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

public event DoWorkEventHandler DoWork

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 Managed Threading Best Practices.

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.

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 for Windows Phone 8.

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;
            // Perform a time consuming operation and report progress.
            worker.ReportProgress(i * 10);

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

© 2017 Microsoft