Partager via


BackgroundWorker.WorkerSupportsCancellation Property

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

Gets or sets a value that indicates whether the BackgroundWorker supports asynchronous cancellation.

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

Syntax

'Declaration
Public Property WorkerSupportsCancellation As Boolean
public bool WorkerSupportsCancellation { get; set; }

Property Value

Type: System.Boolean
true if the BackgroundWorker supports cancellation; otherwise false. The default is false.

Remarks

Set the WorkerSupportsCancellation property to true if you want the BackgroundWorker to support cancellation. When this property is true, you can call the CancelAsync method to interrupt a background operation.

When this property is false, calling the CancelAsync method will cause an InvalidOperationException.

Examples

The following code example demonstrates the use of the WorkerSupportsCancellation property to check whether a background operation allows cancellation before canceling it. To view the complete code for this sample, see How to: Use a Background Worker.

Private Sub buttonCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    If bw.WorkerSupportsCancellation = True Then
        bw.CancelAsync()
    End If
End Sub
private void buttonCancel_Click(object sender, RoutedEventArgs e)
{
    if (bw.WorkerSupportsCancellation == true)
    {
        bw.CancelAsync();
    }
}

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.