다음을 통해 공유


TaskStatus 열거형

정의

Task수명 주기의 현재 단계를 나타냅니다.

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

필드

Name Description
Created 0

작업이 초기화되었지만 아직 예약되지 않았습니다.

WaitingForActivation 1

작업이 .NET 인프라에서 내부적으로 활성화 및 예약되기를 기다리고 있습니다.

WaitingToRun 2

태스크 실행이 예약되었지만 아직 실행이 시작되지 않았습니다.

Running 3

작업이 실행 중이지만 아직 완료되지 않았습니다.

WaitingForChildrenToComplete 4

태스크 실행이 완료되었으며 연결된 자식 작업이 완료되는 것을 암시적으로 기다리고 있습니다.

RanToCompletion 5

작업이 성공적으로 실행을 완료했습니다.

Canceled 6

태스크는 토큰이 신호 상태인 동안 자체 CancellationToken을 사용하여 OperationCanceledException을 throw하여 취소를 승인했거나 태스크가 실행되기 전에 작업의 CancellationToken이 이미 신호를 받았습니다. 자세한 내용은 작업 취소참조하세요.

Faulted 7

처리되지 않은 예외로 인해 작업이 완료되었습니다.

예제

다음 예제에서는 카운터가 2백만 값으로 증가될 때까지 반복되는 20개 작업을 만듭니다. 처음 10개 작업이 200만에 도달하면 취소 토큰이 취소되고 카운터가 200만 개에 도달하지 않은 모든 작업이 취소됩니다. 그런 다음 각 작업의 속성을 검사 Task.Status 하여 작업이 성공적으로 완료되었는지 또는 취소되었는지를 나타냅니다. 완료된 작업의 경우 태스크에서 반환된 값을 표시합니다.

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 멤버를 반환하여 작업의 현재 상태를 나타냅니다.

적용 대상

추가 정보