CancellationToken.IsCancellationRequested 속성

정의

이 토큰의 취소가 요청되었는지 여부를 가져옵니다.

public:
 property bool IsCancellationRequested { bool get(); };
public bool IsCancellationRequested { get; }
member this.IsCancellationRequested : bool
Public ReadOnly Property IsCancellationRequested As Boolean

속성 값

Boolean

이 토큰에 대해 취소가 요청되었으며 true이고, 그러지 않으면 false입니다.

예제

다음은 속성이 반환true될 때까지 IsCancellationRequested 서버 프로세스를 실행하는 간단한 예제입니다.

using System;
using System.Threading;

public class ServerClass
{
   public static void StaticMethod(object obj)
   {
      CancellationToken ct = (CancellationToken)obj;
      Console.WriteLine("ServerClass.StaticMethod is running on another thread.");

      // Simulate work that can be canceled.
      while (!ct.IsCancellationRequested) {
         Thread.SpinWait(50000);
      }
      Console.WriteLine("The worker thread has been canceled. Press any key to exit.");
      Console.ReadKey(true);
   }
}

public class Simple
{
   public static void Main()
   {
      // The Simple class controls access to the token source.
      CancellationTokenSource cts = new CancellationTokenSource();

      Console.WriteLine("Press 'C' to terminate the application...\n");
      // Allow the UI thread to capture the token source, so that it
      // can issue the cancel command.
      Thread t1 = new Thread(() => { if (Console.ReadKey(true).KeyChar.ToString().ToUpperInvariant() == "C")
                                     cts.Cancel(); } );

      // ServerClass sees only the token, not the token source.
      Thread t2 = new Thread(new ParameterizedThreadStart(ServerClass.StaticMethod));
      // Start the UI thread.

      t1.Start();

      // Start the worker thread and pass it the token.
      t2.Start(cts.Token);

      t2.Join();
      cts.Dispose();
   }
}
// The example displays the following output:
//       Press 'C' to terminate the application...
//
//       ServerClass.StaticMethod is running on another thread.
//       The worker thread has been canceled. Press any key to exit.
Imports System.Threading

Public Class ServerClass
   Public Shared Sub StaticMethod(obj As Object)
      Dim ct AS CancellationToken = CType(obj, CancellationToken)
      Console.WriteLine("ServerClass.StaticMethod is running on another thread.")

      ' Simulate work that can be canceled.
      While Not ct.IsCancellationRequested
         Thread.SpinWait(50000)
      End While
      Console.WriteLine("The worker thread has been canceled. Press any key to exit.")
      Console.ReadKey(True)
   End Sub
End Class

Public Class Simple
   Public Shared Sub Main()
      ' The Simple class controls access to the token source.
      Dim cts As New CancellationTokenSource()

      Console.WriteLine("Press 'C' to terminate the application..." + vbCrLf)
      ' Allow the UI thread to capture the token source, so that it
      ' can issue the cancel command.
      Dim t1 As New Thread( Sub()
                               If Console.ReadKey(true).KeyChar.ToString().ToUpperInvariant() = "C" Then
                                  cts.Cancel()
                               End If
                            End Sub)

      ' ServerClass sees only the token, not the token source.
      Dim t2 As New Thread(New ParameterizedThreadStart(AddressOf ServerClass.StaticMethod))

      ' Start the UI thread.
      t1.Start()

      ' Start the worker thread and pass it the token.
      t2.Start(cts.Token)

      t2.Join()
      cts.Dispose()
   End Sub
End Class
' The example displays the following output:
'       Press 'C' to terminate the application...
'
'       ServerClass.StaticMethod is running on another thread.
'       The worker thread has been canceled. Press any key to exit.

이 예제에서는 취소 토큰에 CancellationTokenSource 대한 액세스를 제어하는 개체를 인스턴스화합니다. 그런 다음 두 개의 스레드 프로시저를 정의합니다. 첫 번째는 키보드를 풀하는 람다 식으로 정의되며, "C" 키를 누르면 취소 토큰을 취소된 상태로 설정하기 위해 호출 CancellationTokenSource.Cancel 됩니다. 두 번째는 매개 변수가 있는 메서드로, ServerClass.StaticMethod속성true이 될 때까지 루프를 IsCancellationRequested 실행합니다.

그런 다음 주 스레드는 두 스레드를 시작하고 메서드를 실행하는 ServerClass.StaticMethod 스레드가 종료될 때까지 차단합니다.

설명

이 속성은 처음에 취소된 상태로 생성되는 토큰을 통해 또는 토큰의 연결된 CancellationTokenSource호출을 통해 Cancel 이 토큰에 대해 취소가 요청되었는지 여부를 나타냅니다.

이 속성인 true경우 취소가 요청된 것만 보장합니다. 등록된 모든 처리기가 실행을 완료하거나 취소 요청이 등록된 모든 처리기에 전파를 완료했음을 보장하지는 않습니다. 특히 관련 개체가 동시에 취소되는 경우 추가 동기화가 필요할 수 있습니다.

적용 대상

추가 정보