Task<'TResult> Class
Represents an asynchronous operation that can return a value.
Assembly: mscorlib (in mscorlib.dll)
| 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>, CancellationToken, TaskCreationOptions) | Initializes a new Task<'TResult> with the specified function and creation options. |
![]() | 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<Object, 'TResult>, Object, CancellationToken) | 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. |
![]() | Task<'TResult>(Func<Object, 'TResult>, Object, TaskCreationOptions) | Initializes a new Task<'TResult> with the specified action, state, and options. |
| Name | Description | |
|---|---|---|
![]() | AsyncState | |
![]() | 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 and configuring Task<'TResult> instances. |
![]() | Id | |
![]() | IsCanceled | |
![]() | IsCompleted | |
![]() | IsFaulted | |
![]() | Result | Gets the result value of this Task<'TResult>. |
![]() | Status | Gets the TaskStatus of this task.(Inherited from Task.) |
| 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.) |
| Name | Description | |
|---|---|---|
![]() | DispatcherOperationWait() | Overloaded. Waits indefinitely for the underlying DispatcherOperation to complete.(Defined by TaskExtensions.) |
![]() | DispatcherOperationWait(TimeSpan) | Overloaded. Waits for the specified amount of time for the underlying DispatcherOperation to complete.(Defined by TaskExtensions.) |
![]() | IsDispatcherOperationTask() | Returns a value that indicates whether this Task is associated with a DispatcherOperation.(Defined by TaskExtensions.) |
The Task<'TResult> class represents a single operation that returns a value and that usually executes asynchronously. Task<'TResult> objects are one of the central components of the first introduced in the .NET Framework 4. Because the work performed by a Task<'TResult> object typically executes asynchronously on a thread pool thread rather than synchronously on the main application thread, you can use the Status property, as well as the IsCanceled, IsCompleted, and IsFaulted properties, to determine the state of a task. Most commonly, a lambda expression is used to specify the work that the task is to perform.
Task<'TResult> instances may be created in a variety of ways. The most common approach, which is available starting with the .NET Framework 4.5, is to call the static Task.Run<'TResult>(Func<'TResult>) or Task.Run<'TResult>(Func<'TResult>, CancellationToken) method. These methods provide a simple way to start a task by using default values and without acquiring additional parameters. The following example uses the Task.Run<'TResult>(Func<'TResult>) method to start a task that loops and then displays the number of loop iterations:
An alternative, and the most common way to start a task in the .NET Framework 4, is to call the static TaskFactory.StartNew or TaskFactory<'TResult>.StartNew method. The Task.Factory property returns a TaskFactory object, and the Task<'TResult>.Factory property returns a TaskFactory<'TResult> object. Overloads of their StartNew method let you pass arguments, define task creation options, and specify a task scheduler. The following example uses the TaskFactory<'TResult>.StartNew(Func<'TResult>) method to start a task. It is functionally equivalent to the code in the previous example.
For more complete examples, see Task-based Asynchronous Programming.
The Task<'TResult> class also provides constructors that initialize the task but that do not schedule it for execution. For performance reasons, the Task.Run and StartNew methods are the preferred mechanism for creating and scheduling computational tasks, but for scenarios where task 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.
Starting with desktop apps that target the .NET Framework 4.6, the culture of the thread that creates and invokes a task becomes part of the thread's context. That is, regardless of the current culture of the thread on which the task executes, the current culture of the task is the culture of the calling thread. For apps that target versions of the .NET Framework prior to the .NET Framework 4.6, the culture of the task is the culture of the thread on which the task executes. For more information, see the "Culture and task-based asynchronous operations" section in the CultureInfo topic. Note that Store apps follow the Windows Runtime in setting and getting the default culture.
For operations that do not return a value, you use the Task class.
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
All members of Task<'TResult>, except for Dispose, are thread-safe and may be used from multiple threads concurrently.
.jpeg?cs-save-lang=1&cs-lang=fsharp)
.jpeg?cs-save-lang=1&cs-lang=fsharp)
.jpeg?cs-save-lang=1&cs-lang=fsharp)
.jpeg?cs-save-lang=1&cs-lang=fsharp)
.jpeg?cs-save-lang=1&cs-lang=fsharp)
.jpeg?cs-save-lang=1&cs-lang=fsharp)