Task<TResult> Class
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Represents an asynchronous operation that can return a value.
Assembly: mscorlib (in mscorlib.dll)
The Task<TResult> type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | Task<TResult>(Func<TResult>) | Initializes a new Task<TResult> with the specified function. |
![]() | Task<TResult>(Func<TResult>, CancellationToken) | Initializes a new Task<TResult> with the specified function. |
![]() | Task<TResult>(Func<TResult>, TaskCreationOptions) | Initializes a new Task<TResult> with the specified function and creation options. |
![]() | Task<TResult>(Func<Object, TResult>, Object) | Initializes a new Task<TResult> with the specified function and state. |
![]() | Task<TResult>(Func<TResult>, CancellationToken, TaskCreationOptions) | Initializes a new Task<TResult> with the specified function and creation options. |
![]() | Task<TResult>(Func<Object, TResult>, Object, CancellationToken) | Initializes a new Task<TResult> with the specified action, state, and options. |
![]() | Task<TResult>(Func<Object, TResult>, Object, TaskCreationOptions) | Initializes a new Task<TResult> with the specified action, state, and options. |
![]() | Task<TResult>(Func<Object, TResult>, Object, CancellationToken, TaskCreationOptions) | Initializes a new Task<TResult> 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. (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<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<TResult>. |
![]() | Status | Gets the TaskStatus of this Task. (Inherited from Task.) |
| Name | Description | |
|---|---|---|
![]() | ConfigureAwait | Configures an awaiter used to await this Task<TResult>. |
![]() | ContinueWith(Action<Task>) | Creates a continuation that executes asynchronously when the target Task completes. (Inherited from Task.) |
![]() | ContinueWith(Action<Task<TResult>>) | Creates a continuation that executes asynchronously when the target Task<TResult> completes. |
![]() | ContinueWith(Action<Task>, CancellationToken) | Creates a continuation that executes asynchronously when the target Task completes. (Inherited from Task.) |
![]() | ContinueWith(Action<Task>, TaskContinuationOptions) | Creates a continuation that executes according to the specified TaskContinuationOptions. (Inherited from Task.) |
![]() | ContinueWith(Action<Task>, TaskScheduler) | Creates a continuation that executes asynchronously when the target Task completes. (Inherited from Task.) |
![]() | ContinueWith(Action<Task, Object>, Object) | Creates a continuation that executes when the target Task completes. (Inherited from Task.) |
![]() | ContinueWith(Action<Task<TResult>>, CancellationToken) | Creates a continuation that executes asynchronously when the target Task<TResult> completes. |
![]() | ContinueWith(Action<Task<TResult>>, TaskContinuationOptions) | Creates a continuation that executes according the condition specified in continuationOptions. |
![]() | ContinueWith(Action<Task<TResult>>, TaskScheduler) | Creates a continuation that executes asynchronously when the target Task<TResult> completes. |
![]() | ContinueWith(Action<Task<TResult>, Object>, Object) | Creates a continuation that executes when the target Task<TResult> completes. |
![]() | ContinueWith(Action<Task, Object>, Object, CancellationToken) | Creates a continuation that executes when the target Task completes. (Inherited from Task.) |
![]() | ContinueWith(Action<Task, Object>, Object, TaskContinuationOptions) | Creates a continuation that executes when the target Task completes. (Inherited from Task.) |
![]() | ContinueWith(Action<Task, Object>, Object, TaskScheduler) | Creates a continuation that executes when the target Task completes. (Inherited from Task.) |
![]() | ContinueWith(Action<Task<TResult>, Object>, Object, CancellationToken) | Creates a continuation that executes when the target Task<TResult> completes. |
![]() | ContinueWith(Action<Task<TResult>, Object>, Object, TaskContinuationOptions) | Creates a continuation that executes when the target Task<TResult> completes. |
![]() | ContinueWith(Action<Task<TResult>, Object>, Object, TaskScheduler) | Creates a continuation that executes when the target Task<TResult> completes. |
![]() | ContinueWith(Action<Task>, CancellationToken, TaskContinuationOptions, TaskScheduler) | Creates a continuation that executes according to the specified TaskContinuationOptions. (Inherited from Task.) |
![]() | ContinueWith(Action<Task<TResult>>, CancellationToken, TaskContinuationOptions, TaskScheduler) | Creates a continuation that executes according the condition specified in continuationOptions. |
![]() | ContinueWith(Action<Task, Object>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler) | Creates a continuation that executes when the target Task completes. (Inherited from Task.) |
![]() | ContinueWith(Action<Task<TResult>, Object>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler) | Creates a continuation that executes when the target Task<TResult> completes. |
![]() | ContinueWith<TResult>(Func<Task, TResult>) | Creates a continuation that executes asynchronously when the target Task completes. (Inherited from Task.) |
![]() | ContinueWith<TNewResult>(Func<Task<TResult>, TNewResult>) | Creates a continuation that executes asynchronously when the target Task<TResult> completes. |
![]() | ContinueWith<TResult>(Func<Task, TResult>, CancellationToken) | Creates a continuation that executes asynchronously when the target Task completes. (Inherited from Task.) |
![]() | ContinueWith<TResult>(Func<Task, TResult>, TaskContinuationOptions) | Creates a continuation that executes according to the condition specified in continuationOptions. (Inherited from Task.) |
![]() | ContinueWith<TResult>(Func<Task, TResult>, TaskScheduler) | Creates a continuation that executes asynchronously when the target Task completes. (Inherited from Task.) |
![]() | ContinueWith<TResult>(Func<Task, Object, TResult>, Object) | Creates a continuation that executes when the target Task completes. (Inherited from Task.) |
![]() | ContinueWith<TNewResult>(Func<Task<TResult>, TNewResult>, CancellationToken) | Creates a continuation that executes asynchronously when the target Task<TResult> completes. |
![]() | ContinueWith<TNewResult>(Func<Task<TResult>, TNewResult>, TaskContinuationOptions) | Creates a continuation that executes according the condition specified in continuationOptions. |
![]() | ContinueWith<TNewResult>(Func<Task<TResult>, TNewResult>, TaskScheduler) | Creates a continuation that executes asynchronously when the target Task<TResult> completes. |
![]() | ContinueWith<TNewResult>(Func<Task<TResult>, Object, TNewResult>, Object) | Creates a continuation that executes when the target Task<TResult> completes. |
![]() | ContinueWith<TResult>(Func<Task, Object, TResult>, Object, CancellationToken) | Creates a continuation that executes when the target Task completes. (Inherited from Task.) |
![]() | ContinueWith<TResult>(Func<Task, Object, TResult>, Object, TaskContinuationOptions) | Creates a continuation that executes when the target Task completes. (Inherited from Task.) |
![]() | ContinueWith<TResult>(Func<Task, Object, TResult>, Object, TaskScheduler) | Creates a continuation that executes when the target Task completes. (Inherited from Task.) |
![]() | ContinueWith<TNewResult>(Func<Task<TResult>, Object, TNewResult>, Object, CancellationToken) | Creates a continuation that executes when the target Task<TResult> completes. |
![]() | ContinueWith<TNewResult>(Func<Task<TResult>, Object, TNewResult>, Object, TaskContinuationOptions) | Creates a continuation that executes when the target Task<TResult> completes. |
![]() | ContinueWith<TNewResult>(Func<Task<TResult>, Object, TNewResult>, Object, TaskScheduler) | Creates a continuation that executes when the target Task<TResult> completes. |
![]() | ContinueWith<TResult>(Func<Task, TResult>, CancellationToken, TaskContinuationOptions, TaskScheduler) | Creates a continuation that executes according to the condition specified in continuationOptions. (Inherited from Task.) |
![]() | ContinueWith<TNewResult>(Func<Task<TResult>, TNewResult>, CancellationToken, TaskContinuationOptions, TaskScheduler) | Creates a continuation that executes according 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. (Inherited from Task.) |
![]() | ContinueWith<TNewResult>(Func<Task<TResult>, Object, TNewResult>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler) | Creates a continuation that executes when the target Task<TResult> completes. |
![]() | 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.) |
![]() | GetAwaiter | Gets an awaiter used to await this Task<TResult>. |
![]() | 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.) |
![]() | RunSynchronously() | Runs the Task synchronously on the current TaskScheduler. (Inherited from Task.) |
![]() | RunSynchronously(TaskScheduler) | Runs the Task synchronously on the TaskScheduler provided. (Inherited from Task.) |
![]() | Start() | Starts the Task, scheduling it for execution to the current TaskScheduler. (Inherited from Task.) |
![]() | Start(TaskScheduler) | Starts the Task, scheduling it for execution to the specified TaskScheduler. (Inherited from Task.) |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
![]() | Wait() | Waits for the Task to complete execution. (Inherited from Task.) |
![]() | Wait(CancellationToken) | Waits for the Task to complete execution. (Inherited from Task.) |
![]() | Wait(Int32) | Waits for the Task to complete execution. (Inherited from Task.) |
![]() | Wait(TimeSpan) | Waits for the Task to complete execution. (Inherited from Task.) |
![]() | Wait(Int32, CancellationToken) | Waits for the Task to complete execution. (Inherited from Task.) |
| Name | Description | |
|---|---|---|
![]() | AsAsyncAction | Returns a Windows Runtime asynchronous action that represents a started task. (Defined by WindowsRuntimeSystemExtensions.) |
![]() | AsAsyncOperation<TResult> | Returns a Windows Runtime asynchronous operation that represents a started task that returns a result. (Defined by WindowsRuntimeSystemExtensions.) |
| Name | Description | |
|---|---|---|
![]() ![]() | IAsyncResult::AsyncWaitHandle | Gets a WaitHandle that can be used to wait for the task to complete. (Inherited from Task.) |
![]() ![]() | IAsyncResult::CompletedSynchronously | Gets an indication of whether the operation completed synchronously. (Inherited from Task.) |
Task<TResult> instances may be created in a variety of ways. The most common approach is by using the task's Factory property to retrieve a TaskFactory<TResult> instance that can be used to create tasks for several purposes. For example, to create a Task<TResult> that runs a function, the factory's StartNew method may be used:
// C# var t = Task<int>.Factory.StartNew(() => GenerateResult()); ' Visual Basic Dim t = Task(Of Integer).Factory.StartNew(Function() GenerateResult())
For more complete examples, see [458b5e69-5210-45e5-bc44-3888f86abd6f].
The Task<TResult> class also provides constructors that initialize the task but that do not schedule it for execution. For performance reasons, the 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.






