BackgroundWorker.CancellationPending プロパティ
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
アプリケーションがバックグラウンド操作の取り消しを要求したかどうかを示す値を取得します。
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
{
result = n < 2
? 1
: ComputeFibonacci(n - 1, worker, e) +
ComputeFibonacci(n - 2, worker, e);
// Report progress as a percentage of the total task.
int percentComplete =
(int)(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されている場合は、BackgroundWorkerで CancelAsync メソッドが呼び出されています。
このプロパティはワーカー スレッドが使用することを目的としており、 CancellationPending を定期的にチェックし、 trueに設定されている場合はバックグラウンド操作を中止する必要があります。
適用対象
こちらもご覧ください
- 方法: バックグラウンド で操作を実行する
- 方法: バックグラウンドでファイルをダウンロードする