CancelAsync Method
Collapse the table of content
Expand the table of content

BackgroundWorker.CancelAsync Method

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

Requests cancellation of a pending background operation.

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

public void CancelAsync()

The CancelAsync method submits a request to stop the background operation and sets the CancellationPending property to true.

When you call CancelAsync, your background operation will be able to stop and exit. The operation code should periodically check the CancellationPending property to see whether it has been 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, seeManaged Threading Best Practices.

The following code example demonstrates the use of the CancelAsync method to cancel a background operation. To view the complete code for this sample, see How to use a background worker for Windows Phone 8.

private void buttonCancel_Click(object sender, RoutedEventArgs e)
    if (bw.WorkerSupportsCancellation == true)

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

© 2017 Microsoft