다음을 통해 공유


BackgroundWorker.CancellationPending 속성

정의

애플리케이션에서 백그라운드 작업의 취소를 요청했는지 여부를 나타내는 값을 가져옵니다.

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

속성 값

애플리케이션에서 백그라운드 작업의 취소를 요청했으면 true이고, 그렇지 않으면 false입니다. 기본값은 false입니다.

특성

예제

다음 코드 예제를 사용 CancellationPending 하는 방법에 설명 합니다 취소 상태에 대 한 쿼리를 속성입니다 BackgroundWorker . 이 코드 예제는에 대해 제공 된 큰 예제의 일부는 BackgroundWorker 클래스입니다.

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

설명

가 이trueCancellationPending 메서드가 CancelAsync 에서 BackgroundWorker호출되었습니다.

이 속성은 작업자 스레드에서 사용하기 위한 것으로, 로 설정true되면 주기적으로 검사 CancellationPending 백그라운드 작업을 중단해야 합니다.

적용 대상

추가 정보