BackgroundWorker.CancelAsync Method (System.ComponentModel) (original) (raw)

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Requests cancellation of a pending background operation.

public:
 void CancelAsync();
public void CancelAsync();
member this.CancelAsync : unit -> unit
Public Sub CancelAsync ()

Exceptions

Examples

The following code example demonstrates the use of the CancelAsync method to cancel an asynchronous ("background") operation. This code example is part of a larger example provided for the BackgroundWorker class.

void cancelAsyncButton_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
{  
   // Cancel the asynchronous operation.
   this->backgroundWorker1->CancelAsync();
   
   // Disable the Cancel button.
   cancelAsyncButton->Enabled = false;
}
void cancelAsyncButton_Click(object sender,
    EventArgs e)
{
    // Cancel the asynchronous operation.
    backgroundWorker1.CancelAsync();

    // Disable the Cancel button.
    cancelAsyncButton.Enabled = false;
}
Private Sub cancelAsyncButton_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles cancelAsyncButton.Click
    
    ' Cancel the asynchronous operation.
    Me.backgroundWorker1.CancelAsync()

    ' Disable the Cancel button.
    cancelAsyncButton.Enabled = False
    
End Sub

Remarks

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

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

Applies to

See also