System.Threading.Tasks.Task Class

Represents an asynchronous operation.

See Also: Task

Syntax

[System.Diagnostics.DebuggerDisplay("Id = {Id}, Status = {Status}")]
[System.Diagnostics.DebuggerTypeProxy(typeof(System.Threading.Tasks.TaskDebuggerView))]
public class Task : IAsyncResult, IDisposable

Remarks

Task instances may be created in a variety of ways. The most common approach is by using the task’s Task.Factory property to retrieve a TaskFactory instance that can be used to create tasks for several purposes. For example, to create a Task that runs an action, the factory's TaskFactory.StartNew(Action) method may be used:

Example

// C#
var t = Task.Factory.StartNew(() => DoAction());

' Visual Basic
 Dim t = Task.Factory.StartNew(Function() DoAction())

For more complete examples, see Task Parallelism (Task Parallel Library).

The Task class also provides constructors that initialize the task but that do not schedule it for execution. For performance reasons, the task factory’s TaskFactory.StartNew(Action) method should be the preferred mechanism for creating and scheduling computational tasks, but for scenarios where creation and scheduling must be separated, the constructors may be used, and the task's Task.Start method may then be used to schedule the task for execution at a later time.

For operations that return values, the Task`1 class should be used.

For Debugger Developers

For developers implementing custom debuggers, several internal and private members of task may be useful (these may change from release to release). The m_taskId field serves as the backing store for the Task.Id property, however accessing this field directly from a debugger may be more efficient than accessing the same value through the property's getter method (the s_taskIdCounter counter is used to retrieve the next available ID for a task). Similarly, the m_stateFlags field stores information about the current lifecycle stage of the task, information also accessible through the Task.Status property. The m_action field stores a reference to the task's delegate, and the m_stateObject field stores the async state passed to the task by the developer. Finally, for debuggers that parse stack frames, the InternalWait method serves a potential marker for when a task is entering a wait operation.

Requirements

Namespace: System.Threading.Tasks
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 4.0.0.0

The members of System.Threading.Tasks.Task are listed below.

See Also: Object

Public Constructors

Initializes a new Task with the specified action.

Initializes a new Task with the specified action and CancellationToken.

Initializes a new Task with the specified action and creation options.

Initializes a new Task with the specified action and state.

Initializes a new Task with the specified action and creation options.

Initializes a new Task with the specified action, state, and options.

Initializes a new Task with the specified action, state, and options.

Initializes a new Task with the specified action, state, and options.

Public Properties

[read-only]
AsyncStateObject.

Gets the state object supplied when the Task was created, or null if none was supplied.

[read-only]
CreationOptionsTaskCreationOptions.

Gets the TaskCreationOptions used to create this task.

[read-only]
static
CurrentIdNullable<int>.

Returns the unique ID of the currently executing Task.

[read-only]
ExceptionAggregateException.

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.

[read-only]
static
FactoryTaskFactory.

Provides access to factory methods for creating Task and Task`1 instances.

[read-only]
IdInt32.

Gets a unique ID for this Task instance.

[read-only]
IsCanceledBoolean.

Gets whether this Task instance has completed execution due to being canceled.

[read-only]
IsCompletedBoolean.

Gets whether this Task has completed.

[read-only]
IsFaultedBoolean.

Gets whether the Task completed due to an unhandled exception.

[read-only]
StatusTaskStatus.

Gets the TaskStatus of this task.

Public Methods

ConfigureAwait(Boolean) : ConfiguredTaskAwaitable

Configures an awaiter used to await this Task.

ContinueWith(Action<System.Threading.Tasks.Task>) : Task

Creates a continuation that executes asynchronously when the target Task completes.

ContinueWith(Action<System.Threading.Tasks.Task, object>, Object) : Task

Creates a continuation that executes when the target Task completes.

ContinueWith(Action<System.Threading.Tasks.Task>, CancellationToken) : Task

Creates a continuation that executes asynchronously when the target Task completes.

ContinueWith(Action<System.Threading.Tasks.Task>, TaskContinuationOptions) : Task

Creates a continuation that executes according to the specified TaskContinuationOptions.

ContinueWith(Action<System.Threading.Tasks.Task>, TaskScheduler) : Task

Creates a continuation that executes asynchronously when the target Task completes.

ContinueWith(Action<System.Threading.Tasks.Task, object>, Object, CancellationToken) : Task

Creates a continuation that executes when the target Task completes.

ContinueWith(Action<System.Threading.Tasks.Task, object>, Object, TaskContinuationOptions) : Task

Creates a continuation that executes when the target Task completes.

ContinueWith(Action<System.Threading.Tasks.Task, object>, Object, TaskScheduler) : Task

Creates a continuation that executes when the target Task completes.

ContinueWith(Action<System.Threading.Tasks.Task>, CancellationToken, TaskContinuationOptions, TaskScheduler) : Task

Creates a continuation that executes according to the specified TaskContinuationOptions.

ContinueWith(Action<System.Threading.Tasks.Task, object>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler) : Task

Creates a continuation that executes when the target Task completes.

ContinueWith<TResult>(Func<System.Threading.Tasks.Task, TResult>) : System.Threading.Tasks.Task<TResult>

Creates a continuation that executes asynchronously when the target Task completes.

ContinueWith<TResult>(Func<System.Threading.Tasks.Task, object, TResult>, Object) : System.Threading.Tasks.Task<TResult>

Creates a continuation that executes when the target Task completes.

ContinueWith<TResult>(Func<System.Threading.Tasks.Task, TResult>, CancellationToken) : System.Threading.Tasks.Task<TResult>

Creates a continuation that executes asynchronously when the target Task completes.

ContinueWith<TResult>(Func<System.Threading.Tasks.Task, TResult>, TaskContinuationOptions) : System.Threading.Tasks.Task<TResult>

Creates a continuation that executes according to the condition specified in continuationOptions.

ContinueWith<TResult>(Func<System.Threading.Tasks.Task, TResult>, TaskScheduler) : System.Threading.Tasks.Task<TResult>

Creates a continuation that executes asynchronously when the target Task completes.

ContinueWith<TResult>(Func<System.Threading.Tasks.Task, object, TResult>, Object, CancellationToken) : System.Threading.Tasks.Task<TResult>

Creates a continuation that executes when the target Task completes.

ContinueWith<TResult>(Func<System.Threading.Tasks.Task, object, TResult>, Object, TaskContinuationOptions) : System.Threading.Tasks.Task<TResult>

Creates a continuation that executes when the target Task completes.

ContinueWith<TResult>(Func<System.Threading.Tasks.Task, object, TResult>, Object, TaskScheduler) : System.Threading.Tasks.Task<TResult>

Creates a continuation that executes when the target Task completes.

ContinueWith<TResult>(Func<System.Threading.Tasks.Task, TResult>, CancellationToken, TaskContinuationOptions, TaskScheduler) : System.Threading.Tasks.Task<TResult>

Creates a continuation that executes according to the condition specified in continuationOptions.

ContinueWith<TResult>(Func<System.Threading.Tasks.Task, object, TResult>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler) : System.Threading.Tasks.Task<TResult>

Creates a continuation that executes when the target Task completes.

static
Delay(Int32) : Task

Creates a task that will complete after a time delay.

static
Delay(TimeSpan) : Task

Creates a task that will complete after a time delay.

static
Delay(Int32, CancellationToken) : Task

Creates a task that will complete after a time delay.

static
Delay(TimeSpan, CancellationToken) : Task

Creates a task that will complete after a time delay.

Dispose()

Releases all resources used by the current instance of the Task class.

static
FromResult<TResult>(TResult) : System.Threading.Tasks.Task<TResult>
GetAwaiter() : TaskAwaiter

Gets an awaiter used to await this Task.

static
Run(Action) : Task

Queues the specified work to run on the ThreadPool and returns a task handle for that work.

static
Run(Func<System.Threading.Tasks.Task>) : Task

Queues the specified work to run on the ThreadPool and returns a proxy for the task returned by function.

static
Run(Action, CancellationToken) : Task

Queues the specified work to run on the ThreadPool and returns a task handle for that work.

static
Run(Func<System.Threading.Tasks.Task>, CancellationToken) : Task

Queues the specified work to run on the ThreadPool and returns a proxy for the task returned by function.

static
Run<TResult>(Func<System.Threading.Tasks.Task<TResult>>) : System.Threading.Tasks.Task<TResult>

Queues the specified work to run on the ThreadPool and returns a proxy for the Task(TResult) returned by function.

static
Run<TResult>(Func<TResult>) : System.Threading.Tasks.Task<TResult>

Queues the specified work to run on the ThreadPool and returns a Task(TResult) handle for that work.

static
Run<TResult>(Func<System.Threading.Tasks.Task<TResult>>, CancellationToken) : System.Threading.Tasks.Task<TResult>

Queues the specified work to run on the ThreadPool and returns a proxy for the Task(TResult) returned by function.

static
Run<TResult>(Func<TResult>, CancellationToken) : System.Threading.Tasks.Task<TResult>

Queues the specified work to run on the thread pool and returns a Task(TResult) handle for that work.

RunSynchronously()

Runs the Task synchronously on the current TaskScheduler.

RunSynchronously(TaskScheduler)

Runs the Task synchronously on the TaskScheduler provided.

Start()

Starts the Task, scheduling it for execution to the current TaskScheduler.

Start(TaskScheduler)

Starts the Task, scheduling it for execution to the specified TaskScheduler.

Wait()

Waits for the Task to complete execution.

Wait(Int32) : Boolean

Waits for the Task to complete execution within a specified number of milliseconds.

Wait(CancellationToken)

Waits for the cancellable Task to complete execution.

Wait(TimeSpan) : Boolean

Waits for the Task to complete execution within a specified time interval.

Wait(Int32, CancellationToken) : Boolean

Waits for the cancellable Task to complete execution.

static
WaitAll(params Task[])

Waits for all of the provided Task objects to complete execution.

static
WaitAll(Task[], Int32) : Boolean

Waits for all of the provided Task objects to complete execution within a specified number of milliseconds.

static
WaitAll(Task[], CancellationToken)

Waits for all of the provided cancellable Task objects to complete execution.

static
WaitAll(Task[], TimeSpan) : Boolean

Waits for all of the provided cancellable Task objects to complete execution within a specified time interval.

static
WaitAll(Task[], Int32, CancellationToken) : Boolean

Waits for all of the provided cancellable Task objects to complete execution within a specified number of milliseconds.

static
WaitAny(params Task[]) : Int32

Waits for any of the provided Task objects to complete execution.

static
WaitAny(Task[], Int32) : Int32

Waits for any of the provided Task objects to complete execution within a specified number of milliseconds.

static
WaitAny(Task[], CancellationToken) : Int32

Waits for any of the provided cancellable Task objects to complete execution.

static
WaitAny(Task[], TimeSpan) : Int32

Waits for any of the provided Task objects to complete execution within a specified time interval.

static
WaitAny(Task[], Int32, CancellationToken) : Int32

Waits for any of the provided cancellable Task objects to complete execution within a specified number of milliseconds.

static
WhenAll(IEnumerable<System.Threading.Tasks.Task>) : Task

Creates a task that will complete when all of the supplied tasks have completed.

static
WhenAll(params Task[]) : Task

Creates a task that will complete when all of the supplied tasks have completed.

static
WhenAll<TResult>(IEnumerable<System.Threading.Tasks.Task<TResult>>) : System.Threading.Tasks.Task<TResult[]>

Creates a task that will complete when all of the supplied tasks have completed.

static
WhenAll<TResult>(params System.Threading.Tasks.Task<TResult>[]) : System.Threading.Tasks.Task<TResult[]>

Creates a task that will complete when all of the supplied tasks have completed.

static
WhenAny(IEnumerable<System.Threading.Tasks.Task>) : System.Threading.Tasks.Task<System.Threading.Tasks.Task>

Creates a task that will complete when any of the supplied tasks have completed.

static
WhenAny(params Task[]) : System.Threading.Tasks.Task<System.Threading.Tasks.Task>

Creates a task that will complete when any of the supplied tasks have completed.

static
WhenAny<TResult>(IEnumerable<System.Threading.Tasks.Task<TResult>>) : System.Threading.Tasks.Task<System.Threading.Tasks.Task<TResult>>

Creates a task that will complete when any of the supplied tasks have completed.

static
WhenAny<TResult>(params System.Threading.Tasks.Task<TResult>[]) : System.Threading.Tasks.Task<System.Threading.Tasks.Task<TResult>>

Creates a task that will complete when any of the supplied tasks have completed.

static
Yield() : YieldAwaitable

Creates an awaitable task that asynchronously yields back to the current context when awaited.

Protected Methods

Dispose(Boolean)

Disposes the Task, releasing all of its unmanaged resources.

Explicitly Implemented Interface Members

[read-only]
Task$IAsyncResult.AsyncWaitHandleWaitHandle.

Gets a WaitHandle that can be used to wait for the task to complete.

[read-only]
Task$IAsyncResult.CompletedSynchronouslyBoolean.

Gets an indication of whether the operation completed synchronously.