Udostępnij za pośrednictwem


BackgroundWorker.CancellationPending Właściwość

Definicja

Pobiera wartość wskazującą, czy aplikacja zażądała anulowania operacji w tle.

public:
 property bool CancellationPending { bool get(); };
public bool CancellationPending { get; }
[System.ComponentModel.Browsable(false)]
public bool CancellationPending { get; }
member this.CancellationPending : bool
[<System.ComponentModel.Browsable(false)>]
member this.CancellationPending : bool
Public ReadOnly Property CancellationPending As Boolean

Wartość właściwości

true jeśli aplikacja zażądała anulowania operacji w tle; w przeciwnym razie , false. Wartość domyślna to false.

Atrybuty

Przykłady

Poniższy przykład kodu przedstawia użycie CancellationPending właściwości w celu wykonania zapytania BackgroundWorker o jego stan anulowania. Ten przykład kodu jest częścią większego przykładu podanego BackgroundWorker dla klasy.

// 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->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 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 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 Then
    e.Cancel = True
Else
    If n < 2 Then
        result = 1
    Else
        result = ComputeFibonacci(n - 1, worker, e) + _
                 ComputeFibonacci(n - 2, worker, e)
    End If

    ' Report progress as a percentage of the total task.
    Dim percentComplete As Integer = _
        CSng(n) / CSng(numberToCompute) * 100
    If percentComplete > highestPercentageReached Then
        highestPercentageReached = percentComplete
        worker.ReportProgress(percentComplete)
    End If

End If

Uwagi

Jeśli CancellationPending jest to true, CancelAsync metoda została wywołana w obiekcie BackgroundWorker.

Ta właściwość jest przeznaczona do użycia przez wątek procesu roboczego, który powinien okresowo sprawdzać CancellationPending i przerywać operację w tle, gdy jest ustawiona na true.

Dotyczy

Zobacz też