Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

BackgroundWorker.CancellationPending Property

Gets a value indicating whether the application has requested cancellation of a background operation.

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

public bool CancellationPending { get; }
/** @property */
public boolean get_CancellationPending ()

public function get CancellationPending () : boolean

Not applicable.

Property Value

true if the application has requested cancellation of a background operation; otherwise, false. The default is false.

If CancellationPending is true, then the CancelAsync method has been called on the BackgroundWorker.

This property is meant for use by the worker thread, which should periodically check CancellationPending and abort the background operation when it is set to true.

The following code example demonstrates the use of the CancellationPending property to query a BackgroundWorker about its cancellation state. This code example is part of a larger example provided for the BackgroundWorker class.

// Abort the operation if the user has canceled.
// Note that a call to CancelAsync may have set 
// CancellationPending to true just after the
// last invocation of this method exits, so this 
// code will not have the opportunity to set the 
// DoWorkEventArgs.Cancel flag to true. This means
// that RunWorkerCompletedEventArgs.Cancelled will
// not be set to true in your RunWorkerCompleted
// event handler. This is a race condition.

if (worker.CancellationPending)
{   
    e.Cancel = true;
}
else
{   
    if (n < 2)
    {   
        result = 1;
    }
    else
    {   
        result = ComputeFibonacci(n - 1, worker, e) + 
                 ComputeFibonacci(n - 2, worker, e);
    }

    // Report progress as a percentage of the total task.
    int percentComplete = 
        (int)((float)n / (float)numberToCompute * 100);
    if (percentComplete > highestPercentageReached)
    {
        highestPercentageReached = percentComplete;
        worker.ReportProgress(percentComplete);
    }
}

// Abort the operation if the user has cancelled.
// Note that a call to CancelAsync may have set 
// CancellationPending to true just after the
// last invocation of this method exits, so this 
// code will not have the opportunity to set the 
// DoWorkEventArgs.Cancel flag to true. This means
// that RunWorkerCompletedEventArgs.Cancelled will
// not be set to true in your RunWorkerCompleted
// event handler. This is a race condition.
if (worker.get_CancellationPending()) {

    e.set_Cancel(true);
}
else {

    if (n < 2) {
    
        result = 1;
    }
    else {
    
        result = ComputeFibonacci(n - 1, worker, e) 
            + ComputeFibonacci(n - 2, worker, e);
    }

    // Report progress as a percentage of the total task.
    int percentComplete=(int)((float)(n)/(float)(numberToCompute)* 100);

    if (percentComplete > highestPercentageReached) {            
        highestPercentageReached = percentComplete;
        worker.ReportProgress(percentComplete);
    }
}

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft