Task Class
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Represents an asynchronous operation.
Assembly: mscorlib (in mscorlib.dll)
The Task type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | Task(Action) | Initializes a new Task with the specified action. |
![]() | Task(Action, CancellationToken) | Initializes a new Task with the specified action and CancellationToken. |
![]() | Task(Action, TaskCreationOptions) | Initializes a new Task with the specified action and creation options. |
![]() | Task(Action<Object>, Object) | Initializes a new Task with the specified action and state. |
![]() | Task(Action, CancellationToken, TaskCreationOptions) | Initializes a new Task with the specified action and creation options. |
![]() | Task(Action<Object>, Object, CancellationToken) | Initializes a new Task with the specified action, state, and options. |
![]() | Task(Action<Object>, Object, TaskCreationOptions) | Initializes a new Task with the specified action, state, and options. |
![]() | Task(Action<Object>, Object, CancellationToken, TaskCreationOptions) | Initializes a new Task with the specified action, state, and options. |
| Name | Description | |
|---|---|---|
![]() | AsyncState | Gets the state object supplied when the Task was created, or null if none was supplied. |
![]() | CreationOptions | Gets the TaskCreationOptions used to create this task. |
![]() ![]() | CurrentId | Returns the unique ID of the currently executing 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. |
![]() ![]() | Factory | Provides access to factory methods for creating Task and Task<TResult> instances. |
![]() | Id | Gets a unique ID for this Task instance. |
![]() | IsCanceled | Gets whether this Task instance has completed execution due to being canceled. |
![]() | IsCompleted | Gets whether this Task has completed. |
![]() | IsFaulted | Gets whether the Task completed due to an unhandled exception. |
![]() | Status | Gets the TaskStatus of this Task. |
| Name | Description | |
|---|---|---|
![]() | ConfigureAwait | Configures an awaiter used to await this Task. |
![]() | ContinueWith(Action<Task>) | Creates a continuation that executes asynchronously when the target Task completes. |
![]() | ContinueWith(Action<Task>, CancellationToken) | Creates a continuation that executes asynchronously when the target Task completes. |
![]() | ContinueWith(Action<Task>, TaskContinuationOptions) | Creates a continuation that executes according to the specified TaskContinuationOptions. |
![]() | ContinueWith(Action<Task>, TaskScheduler) | Creates a continuation that executes asynchronously when the target Task completes. |
![]() | ContinueWith(Action<Task, Object>, Object) | Creates a continuation that executes when the target Task completes. |
![]() | ContinueWith(Action<Task, Object>, Object, CancellationToken) | Creates a continuation that executes when the target Task completes. |
![]() | ContinueWith(Action<Task, Object>, Object, TaskContinuationOptions) | Creates a continuation that executes when the target Task completes. |
![]() | ContinueWith(Action<Task, Object>, Object, TaskScheduler) | Creates a continuation that executes when the target Task completes. |
![]() | ContinueWith(Action<Task>, CancellationToken, TaskContinuationOptions, TaskScheduler) | Creates a continuation that executes according to the specified TaskContinuationOptions. |
![]() | ContinueWith(Action<Task, Object>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler) | Creates a continuation that executes when the target Task completes. |
![]() | ContinueWith<TResult>(Func<Task, TResult>) | Creates a continuation that executes asynchronously when the target Task completes. |
![]() | ContinueWith<TResult>(Func<Task, TResult>, CancellationToken) | Creates a continuation that executes asynchronously when the target Task completes. |
![]() | ContinueWith<TResult>(Func<Task, TResult>, TaskContinuationOptions) | Creates a continuation that executes according to the condition specified in continuationOptions. |
![]() | ContinueWith<TResult>(Func<Task, TResult>, TaskScheduler) | Creates a continuation that executes asynchronously when the target Task completes. |
![]() | ContinueWith<TResult>(Func<Task, Object, TResult>, Object) | Creates a continuation that executes when the target Task completes. |
![]() | ContinueWith<TResult>(Func<Task, Object, TResult>, Object, CancellationToken) | Creates a continuation that executes when the target Task completes. |
![]() | ContinueWith<TResult>(Func<Task, Object, TResult>, Object, TaskContinuationOptions) | Creates a continuation that executes when the target Task completes. |
![]() | ContinueWith<TResult>(Func<Task, Object, TResult>, Object, TaskScheduler) | Creates a continuation that executes when the target Task completes. |
![]() | ContinueWith<TResult>(Func<Task, TResult>, CancellationToken, TaskContinuationOptions, TaskScheduler) | Creates a continuation that executes according to the condition specified in continuationOptions. |
![]() | ContinueWith<TResult>(Func<Task, Object, TResult>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler) | Creates a continuation that executes when the target Task completes. |
![]() ![]() | Delay(Int32) | Creates a Task that will complete after a time delay. |
![]() ![]() | Delay(TimeSpan) | Creates a Task that will complete after a time delay. |
![]() ![]() | Delay(Int32, CancellationToken) | Creates a Task that will complete after a time delay. |
![]() ![]() | Delay(TimeSpan, CancellationToken) | Creates a Task that will complete after a time delay. |
![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) |
![]() ![]() | FromResult<TResult> | Creates a Task<TResult> that's completed successfully with the specified result. |
![]() | GetAwaiter | Gets an awaiter used to await this Task. |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() ![]() | Run(Action) | Queues the specified work to run on the ThreadPool and returns a Task handle for that work. |
![]() ![]() | Run(Func<Task>) | Queues the specified work to run on the ThreadPool and returns a proxy for the Task returned by function. |
![]() ![]() | Run(Action, CancellationToken) | Queues the specified work to run on the ThreadPool and returns a Task handle for that work. |
![]() ![]() | Run(Func<Task>, CancellationToken) | Queues the specified work to run on the ThreadPool and returns a proxy for the Task returned by function. |
![]() ![]() | Run<TResult>(Func<Task<TResult>>) | Queues the specified work to run on the ThreadPool and returns a proxy for the Task(TResult) returned by function. |
![]() ![]() | Run<TResult>(Func<TResult>) | Queues the specified work to run on the ThreadPool and returns a Task(TResult) handle for that work. |
![]() ![]() | Run<TResult>(Func<Task<TResult>>, CancellationToken) | Queues the specified work to run on the ThreadPool and returns a proxy for the Task(TResult) returned by function. |
![]() ![]() | Run<TResult>(Func<TResult>, CancellationToken) | Queues the specified work to run on the ThreadPool 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. |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
![]() | Wait() | Waits for the Task to complete execution. |
![]() | Wait(CancellationToken) | Waits for the Task to complete execution. |
![]() | Wait(Int32) | Waits for the Task to complete execution. |
![]() | Wait(TimeSpan) | Waits for the Task to complete execution. |
![]() | Wait(Int32, CancellationToken) | Waits for the Task to complete execution. |
![]() ![]() | WaitAll(array<Task>) | Waits for all of the provided Task objects to complete execution. |
![]() ![]() | WaitAll(array<Task>, Int32) | Waits for all of the provided Task objects to complete execution. |
![]() ![]() | WaitAll(array<Task>, CancellationToken) | Waits for all of the provided Task objects to complete execution. |
![]() ![]() | WaitAll(array<Task>, TimeSpan) | Waits for all of the provided Task objects to complete execution. |
![]() ![]() | WaitAll(array<Task>, Int32, CancellationToken) | Waits for all of the provided Task objects to complete execution. |
![]() ![]() | WaitAny(array<Task>) | Waits for any of the provided Task objects to complete execution. |
![]() ![]() | WaitAny(array<Task>, Int32) | Waits for any of the provided Task objects to complete execution. |
![]() ![]() | WaitAny(array<Task>, CancellationToken) | Waits for any of the provided Task objects to complete execution. |
![]() ![]() | WaitAny(array<Task>, TimeSpan) | Waits for any of the provided Task objects to complete execution. |
![]() ![]() | WaitAny(array<Task>, Int32, CancellationToken) | Waits for any of the provided Task objects to complete execution. |
![]() ![]() | WhenAll(IEnumerable<Task>) | Creates a task that will complete when all of the supplied tasks have completed. |
![]() ![]() | WhenAll(array<Task>) | Creates a task that will complete when all of the supplied tasks have completed. |
![]() ![]() | WhenAll<TResult>(IEnumerable<Task<TResult>>) | Creates a task that will complete when all of the supplied tasks have completed. |
![]() ![]() | WhenAll<TResult>(array<Task<TResult>>) | Creates a task that will complete when all of the supplied tasks have completed. |
![]() ![]() | WhenAny(IEnumerable<Task>) | Creates a task that will complete when any of the supplied tasks have completed. |
![]() ![]() | WhenAny(array<Task>) | Creates a task that will complete when any of the supplied tasks have completed. |
![]() ![]() | WhenAny<TResult>(IEnumerable<Task<TResult>>) | Creates a task that will complete when any of the supplied tasks have completed. |
![]() ![]() | WhenAny<TResult>(array<Task<TResult>>) | Creates a task that will complete when any of the supplied tasks have completed. |
![]() ![]() | Yield | Creates an awaitable task that asynchronously yields back to the current context when awaited. |
| Name | Description | |
|---|---|---|
![]() | AsAsyncAction | Returns a Windows Runtime asynchronous action that represents a started task. (Defined by WindowsRuntimeSystemExtensions.) |
| Name | Description | |
|---|---|---|
![]() ![]() | IAsyncResult::AsyncWaitHandle | Gets a WaitHandle that can be used to wait for the task to complete. |
![]() ![]() | IAsyncResult::CompletedSynchronously | Gets an indication of whether the operation completed synchronously. |
Task instances may be created in a variety of ways. The most common approach is by using the Task type's 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 StartNew method may be used:
// C# var t = Task.Factory.StartNew(() => DoAction()); ' Visual Basic Dim t = Task.Factory.StartNew(Function() DoAction())
For more complete examples, see [458b5e69-5210-45e5-bc44-3888f86abd6f].
The Task class also provides constructors that initialize the Task but that do not schedule it for execution. For performance reasons, TaskFactory's StartNew 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 Start method may then be used to schedule the task for execution at a later time.
For operations that return values, the Task<TResult> 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 Int32 m_taskId field serves as the backing store for the 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 Int32 counter is used to retrieve the next available ID for a Task). Similarly, the Int32 m_stateFlags field stores information about the current lifecycle stage of the Task, information also accessible through the Status property. The m_action System.Object field stores a reference to the Task's delegate, and the m_stateObject System.Object 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.
The following example shows how to start a task by using the TaskFactory::StartNew method:






