TaskStatus 열거형

정의

Task의 수명 주기에서 현재 단계를 나타냅니다.Represents the current stage in the lifecycle of a Task.

public enum class TaskStatus
public enum TaskStatus
type TaskStatus = 
Public Enum TaskStatus
상속
TaskStatus

필드

Canceled 6

작업을 실행하기 전에 토큰이 신호를 받은 상태이거나 작업의 CancellationToken이 이미 신호를 받은 상태에서 자체 CancellationToken과 함께 OperationCanceledException을 throw하여 작업이 취소를 승인했습니다.The task acknowledged cancellation by throwing an OperationCanceledException with its own CancellationToken while the token was in signaled state, or the task's CancellationToken was already signaled before the task started executing. 자세한 내용은 작업 취소를 참조하세요.For more information, see Task Cancellation.

Created 0

작업이 초기화되었지만 예약되지는 않았습니다.The task has been initialized but has not yet been scheduled.

Faulted 7

작업이 처리되지 않은 예외로 인해 완료되었습니다.The task completed due to an unhandled exception.

RanToCompletion 5

작업이 실행을 완료했습니다.The task completed execution successfully.

Running 3

작업이 실행되고 있지만 완료되지 않았습니다.The task is running but has not yet completed.

WaitingForActivation 1

작업이 .NET Framework 인프라를 통해 내부적으로 활성화되고 예약되기 위해 대기 중입니다.The task is waiting to be activated and scheduled internally by the .NET Framework infrastructure.

WaitingForChildrenToComplete 4

작업이 실행을 마쳤지만 연결된 자식 작업이 완료되기까지 암시적으로 대기 중입니다.The task has finished executing and is implicitly waiting for attached child tasks to complete.

WaitingToRun 2

작업이 실행되도록 예약되었지만 아직 실행되지 않았습니다.The task has been scheduled for execution but has not yet begun executing.

예제

다음 예제에서는 2 백만로 카운터가 증가 될 때까지 반복 하는 20 개의 작업을 만듭니다.The following example creates 20 tasks that will loop until a counter is incremented to a value of 2 million. 처음 10 개의 작업에 도달 하면 2 백만, 취소 토큰이 취소 되 고 해당 카운터 2 백만 도달 하지 하는 모든 작업이 취소 됩니다.When the first 10 tasks reach 2 million, the cancellation token is cancelled, and any tasks whose counters have not reached 2 million are cancelled. 이 예제에서는 다음 검사를 Task.Status 작업이 성공적으로 완료 되거나 취소 되었습니다 여부를 나타내는 각 태스크의 속성입니다.The example then examines the Task.Status property of each task to indicate whether the task has completed successfully or been cancelled. 완료에 대해 작업에서 반환 값을 표시 합니다.For those that have completed, it displays the value returned by the task.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      var tasks = new List<Task<int>>();
      var source = new CancellationTokenSource();
      var token = source.Token;
      int completedIterations = 0;

      for (int n = 0; n <= 19; n++)
         tasks.Add(Task.Run( () => { int iterations = 0;
                                     for (int ctr = 1; ctr <= 2000000; ctr++) {
                                         token.ThrowIfCancellationRequested();
                                         iterations++;
                                     }
                                     Interlocked.Increment(ref completedIterations);
                                     if (completedIterations >= 10)
                                        source.Cancel();
                                     return iterations; }, token));

      Console.WriteLine("Waiting for the first 10 tasks to complete...\n");
      try  {
         Task.WaitAll(tasks.ToArray());
      }
      catch (AggregateException) {
         Console.WriteLine("Status of tasks:\n");
         Console.WriteLine("{0,10} {1,20} {2,14:N0}", "Task Id",
                           "Status", "Iterations");
         foreach (var t in tasks)
            Console.WriteLine("{0,10} {1,20} {2,14}",
                              t.Id, t.Status,
                              t.Status != TaskStatus.Canceled ? t.Result.ToString("N0") : "n/a");
      }
   }
}
// The example displays output like the following:
//    Waiting for the first 10 tasks to complete...
//    Status of tasks:
//
//       Task Id               Status     Iterations
//             1      RanToCompletion      2,000,000
//             2      RanToCompletion      2,000,000
//             3      RanToCompletion      2,000,000
//             4      RanToCompletion      2,000,000
//             5      RanToCompletion      2,000,000
//             6      RanToCompletion      2,000,000
//             7      RanToCompletion      2,000,000
//             8      RanToCompletion      2,000,000
//             9      RanToCompletion      2,000,000
//            10             Canceled            n/a
//            11             Canceled            n/a
//            12             Canceled            n/a
//            13             Canceled            n/a
//            14             Canceled            n/a
//            15             Canceled            n/a
//            16      RanToCompletion      2,000,000
//            17             Canceled            n/a
//            18             Canceled            n/a
//            19             Canceled            n/a
//            20             Canceled            n/a
Imports System.Collections.Generic
Imports System.Threading
Imports System.Threading.Tasks

Module Example

   Public Sub Main()
      Dim tasks As New List(Of Task(Of Integer))()
      Dim source As New CancellationTokenSource
      Dim token As CancellationToken = source.Token
      Dim completedIterations As Integer = 0
      
      For n As Integer = 0 To 19
         tasks.Add(Task.Run( Function()
                                Dim iterations As Integer= 0
                                For ctr As Long = 1 To 2000000
                                   token.ThrowIfCancellationRequested()
                                   iterations += 1
                                Next
                                Interlocked.Increment(completedIterations)
                                If completedIterations >= 10 Then source.Cancel()
                                Return iterations
                             End Function, token))
      Next

      Console.WriteLine("Waiting for the first 10 tasks to complete... ")
      Try
         Task.WaitAll(tasks.ToArray())
      Catch e As AggregateException
         Console.WriteLine("Status of tasks:")
         Console.WriteLine()
         Console.WriteLine("{0,10} {1,20} {2,14}", "Task Id",
                           "Status", "Iterations")
         For Each t In tasks
            Console.WriteLine("{0,10} {1,20} {2,14}",
                              t.Id, t.Status,
                              If(t.Status <> TaskStatus.Canceled,
                                 t.Result.ToString("N0"), "n/a"))
         Next
      End Try
   End Sub
End Module
' The example displays output like the following:
'    Waiting for the first 10 tasks to complete...
'    Status of tasks:
'
'       Task Id               Status     Iterations
'             1      RanToCompletion      2,000,000
'             2      RanToCompletion      2,000,000
'             3      RanToCompletion      2,000,000
'             4      RanToCompletion      2,000,000
'             5      RanToCompletion      2,000,000
'             6      RanToCompletion      2,000,000
'             7      RanToCompletion      2,000,000
'             8      RanToCompletion      2,000,000
'             9      RanToCompletion      2,000,000
'            10             Canceled            n/a
'            11             Canceled            n/a
'            12             Canceled            n/a
'            13             Canceled            n/a
'            14             Canceled            n/a
'            15             Canceled            n/a
'            16      RanToCompletion      2,000,000
'            17             Canceled            n/a
'            18             Canceled            n/a
'            19             Canceled            n/a
'            20             Canceled            n/a

설명

합니다 Task.Status 속성의 멤버를 반환 합니다 TaskStatus 작업의 현재 상태를 나타내는 열거형입니다.The Task.Status property returns a member of the TaskStatus enumeration to indicate the task's current status.

적용 대상

추가 정보