- What are properties of a task?
- What does the Dispose() method do?
- What is the tasks ID?
- What are the statuses of a task?
Each Task(TResult) class has following properties?
|AsyncState||Gets the state object supplied when the Task was created, or null if none was supplied. (Inherited from Task.)|
|CreationOptions||Gets the TaskCreationOptions used to create this task. (Inherited from Task.)|
|Exception||Gets the AggregateException that caused the Task to end prematurely. If the Task completed successfully or has not yet thrown any exceptions, this will return null. (Inherited from Task.)|
|Factory||Provides access to factory methods for creating Task<(Of <(TResult>)>) instances.|
|Id||Gets a unique ID for this Task instance. (Inherited from Task.)|
|IsCanceled||Gets whether this Task instance has completed execution due to being canceled. (Inherited from Task.)|
|IsCompleted||Gets whether this Task has completed. (Inherited from Task.)|
|IsFaulted||Gets whether the Task completed due to an unhandled exception. (Inherited from Task.)|
|Result||Gets the result value of this Task<(Of <(TResult>)>).|
|Status||Gets the TaskStatus of this Task. (Inherited from Task.)|
As we can see the Task.Dispose() method sets the ManualResetEventSlim object and calls its Dispose() method.
protected virtual void Dispose(bool disposing)
throw new InvalidOperationException(
ManualResetEventSlim completionEvent = this.m_completionEvent;
if (completionEvent != null)
this.m_completionEvent = null;
this.m_stateFlags |= 0x40000;
The id of a task is calculated on the time of request.
public int Id
if (this.m_taskId == 0)
int num = 0;
num = Interlocked.Increment(ref s_taskIdCounter);
while (num == 0);
Interlocked.CompareExchange(ref this.m_taskId, num, 0);
If we query the CurrentId property while the task is not running it returns null.
public static int? CurrentId
Task internalCurrent = InternalCurrent;
if (internalCurrent != null)
return new int?(internalCurrent.Id);
We can obtain a current of a task requesting the Status property.
|Created||The task has been initialized but has not yet been scheduled.|
|WaitingForActivation||The task is waiting to be activated and scheduled internally by the .NET Framework infrastructure.|
|WaitingToRun||The task has been scheduled for execution but has not yet begun executing.|
|Running||The task is running but has not yet completed.|
|WaitingForChildrenToComplete||The task has finished executing and is implicitly waiting for attached child tasks to complete.|
|RanToCompletion||The task completed execution successfully.|
|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.|
|Faulted||The task completed due to an unhandled exception.|