TaskStatus Enumeration
Represents the current stage in the lifecycle of a Task.
Assembly: mscorlib (in mscorlib.dll)
| Member name | Description | |
|---|---|---|
| Canceled | 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 | The task has been initialized but has not yet been scheduled. | |
| Faulted | The task completed due to an unhandled exception. | |
| RanToCompletion | The task completed execution successfully. | |
| Running | The task is running but has not yet completed. | |
| WaitingForActivation | The task is waiting to be activated and scheduled internally by the .NET Framework infrastructure. | |
| WaitingForChildrenToComplete | The task has finished executing and is implicitly waiting for attached child tasks to complete. | |
| WaitingToRun | The task has been scheduled for execution but has not yet begun executing. |
The Task.Status property returns a member of the TaskStatus enumeration to indicate the task's current status.
The following example creates 20 tasks that will loop until a counter is incremented to a value of 2 million. 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. 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.
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
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 5.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1