System.ComponentModel.BackgroundWorker.CancelAsync Method

Requests cancellation of a pending background operation.

Syntax

public void CancelAsync ()

Remarks

BackgroundWorker.CancelAsync submits a request to terminate the pending background operation and sets the BackgroundWorker.CancellationPending property to true.

When you call BackgroundWorker.CancelAsync, your worker method has an opportunity to stop its execution and exit. The worker code should periodically check the BackgroundWorker.CancellationPending property to see if it has been set to true.

Note:

Be aware that your code in the BackgroundWorker.DoWork event handler may finish its work as a cancellation request is being made, and your polling loop may miss BackgroundWorker.CancellationPending being set to true. In this case, the AsyncCompletedEventArgs.Cancelled flag of System.ComponentModel.RunWorkerCompletedEventArgs in your BackgroundWorker.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.

Requirements

Namespace: System.ComponentModel
Assembly: System (in System.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0
Since: .NET 2.0