BackgroundWorker.CancellationPending プロパティ

定義

アプリケーションがバックグラウンド操作のキャンセルを要求したかどうかを示す値を取得します。Gets a value indicating whether the application has requested cancellation of a background operation.

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

プロパティ値

Boolean

アプリケーションがバックグラウンド操作のキャンセルを要求した場合は true。それ以外の場合は falsetrue if the application has requested cancellation of a background operation; otherwise, false. 既定値は、false です。The default is false.

属性

次のコード例は、プロパティを使用し CancellationPending て、キャンセル状態に関するを照会する方法を示して BackgroundWorker います。The following code example demonstrates the use of the CancellationPending property to query a BackgroundWorker about its cancellation state. このコード例は、BackgroundWorker クラスのために提供されている大規模な例の一部です。This code example is part of a larger example provided for the BackgroundWorker class.

// 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

注釈

がの場合、 CancellationPending true メソッドは CancelAsync で呼び出されてい BackgroundWorker ます。If CancellationPending is true, then the CancelAsync method has been called on the BackgroundWorker.

このプロパティは、ワーカースレッドによって使用されることを意図しています。これは、に設定されている場合に、バックグラウンド操作を定期的にチェックして中止する必要があり CancellationPending true ます。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.

適用対象

こちらもご覧ください