Biblioteca de clases de .NET Framework
BackgroundWorker.CancellationPending (Propiedad)

Nota: esta propiedad es nueva en la versión 2.0 de .NET Framework.

Obtiene un valor que indica si la aplicación ha solicitado la cancelación de una operación en segundo plano.

Espacio de nombres: System.ComponentModel
Ensamblado: System (en system.dll)

Sintaxis

Visual Basic (Declaración)
Public ReadOnly Property CancellationPending As Boolean
Visual Basic (Uso)
Dim instance As BackgroundWorker
Dim value As Boolean

value = instance.CancellationPending
C#
public bool CancellationPending { get; }
C++
public:
property bool CancellationPending {
    bool get ();
}
J#
/** @property */
public boolean get_CancellationPending ()
JScript
public function get CancellationPending () : boolean

Valor de propiedad

true si la aplicación ha solicitado la cancelación de una operación en segundo plano; de lo contrario, false. El valor predeterminado es false.
Comentarios

Si CancellationPending es true, significa que se ha llamado al método CancelAsync en el objeto BackgroundWorker.

Esta propiedad está pensada para que la utilice el subproceso de trabajo, que debería comprobar CancellationPending periódicamente y anular la operación en segundo plano cuando se establezca en true.

Ejemplo

En el ejemplo de código siguiente se muestra el uso de la propiedad CancellationPending para consultar el estado de cancelación de BackgroundWorker. Este ejemplo de código forma parte de un ejemplo más extenso referente a la clase BackgroundWorker.

Visual Basic
' 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
C#
// 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);
    }
}
C++
// 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 );
   }
}
J#
// 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);
    }
}
Plataformas

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

.NET Framework no admite todas las versiones de cada plataforma. Para obtener una lista de las versiones admitidas, vea Requisitos del sistema.

Información de versión

.NET Framework

Compatible con: 2.0
Vea también

Etiquetas :


Page view tracker