Export (0) Print
Expand All

Task<TResult> Class

Represents an asynchronous operation that can return a value.

System.Object
  System.Threading.Tasks.Task
    System.Threading.Tasks.Task<TResult>

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

[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true, 
	ExternalThreading = true)]
public class Task<TResult> : Task

Type Parameters

TResult

The type of the result produced by this Task<TResult>.

The Task<TResult> type exposes the following members.

  NameDescription
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsTask<TResult>(Func<TResult>)Initializes a new Task<TResult> with the specified function.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsTask<TResult>(Func<TResult>, CancellationToken)Initializes a new Task<TResult> with the specified function.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsTask<TResult>(Func<TResult>, TaskCreationOptions)Initializes a new Task<TResult> with the specified function and creation options.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsTask<TResult>(Func<Object, TResult>, Object)Initializes a new Task<TResult> with the specified function and state.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsTask<TResult>(Func<TResult>, CancellationToken, TaskCreationOptions)Initializes a new Task<TResult> with the specified function and creation options.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsTask<TResult>(Func<Object, TResult>, Object, CancellationToken)Initializes a new Task<TResult> with the specified action, state, and options.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsTask<TResult>(Func<Object, TResult>, Object, TaskCreationOptions)Initializes a new Task<TResult> with the specified action, state, and options.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsTask<TResult>(Func<Object, TResult>, Object, CancellationToken, TaskCreationOptions)Initializes a new Task<TResult> with the specified action, state, and options.
Top

  NameDescription
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsAsyncStateGets the state object supplied when the Task was created, or null if none was supplied. (Inherited from Task.)
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsCreationOptionsGets the TaskCreationOptions used to create this task. (Inherited from Task.)
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsExceptionGets 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.)
Public propertyStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsFactoryProvides access to factory methods for creating Task<TResult> instances.
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsIdGets a unique ID for this Task instance. (Inherited from Task.)
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsIsCanceledGets whether this Task instance has completed execution due to being canceled. (Inherited from Task.)
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsIsCompletedGets whether this Task has completed. (Inherited from Task.)
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsIsFaultedGets whether the Task completed due to an unhandled exception. (Inherited from Task.)
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsResultGets the result value of this Task<TResult>.
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsStatusGets the TaskStatus of this task. (Inherited from Task.)
Top

  NameDescription
Public methodSupported in .NET for Windows Store appsConfigureAwaitConfigures an awaiter used to await this Task<TResult>.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsContinueWith(Action<Task>)Creates a continuation that executes asynchronously when the target Task completes. (Inherited from Task.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsContinueWith(Action<Task<TResult>>)Creates a continuation that executes asynchronously when the target Task<TResult> completes.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsContinueWith(Action<Task>, CancellationToken)Creates a continuation that executes asynchronously when the target Task completes. (Inherited from Task.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsContinueWith(Action<Task>, TaskContinuationOptions)Creates a continuation that executes according to the specified TaskContinuationOptions. (Inherited from Task.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsContinueWith(Action<Task>, TaskScheduler)Creates a continuation that executes asynchronously when the target Task completes. (Inherited from Task.)
Public methodSupported in .NET for Windows Store appsContinueWith(Action<Task, Object>, Object)Creates a continuation that executes when the target Task completes. (Inherited from Task.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsContinueWith(Action<Task<TResult>>, CancellationToken)Creates a continuation that executes asynchronously when the target Task<TResult> completes.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsContinueWith(Action<Task<TResult>>, TaskContinuationOptions)Creates a continuation that executes according the condition specified in continuationOptions.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsContinueWith(Action<Task<TResult>>, TaskScheduler)Creates a continuation that executes asynchronously when the target Task<TResult> completes.
Public methodSupported in .NET for Windows Store appsContinueWith(Action<Task<TResult>, Object>, Object)Creates a continuation that executes when the target Task<TResult> completes.
Public methodSupported in .NET for Windows Store appsContinueWith(Action<Task, Object>, Object, CancellationToken)Creates a continuation that executes when the target Task completes. (Inherited from Task.)
Public methodSupported in .NET for Windows Store appsContinueWith(Action<Task, Object>, Object, TaskContinuationOptions)Creates a continuation that executes when the target Task completes. (Inherited from Task.)
Public methodSupported in .NET for Windows Store appsContinueWith(Action<Task, Object>, Object, TaskScheduler)Creates a continuation that executes when the target Task completes. (Inherited from Task.)
Public methodSupported in .NET for Windows Store appsContinueWith(Action<Task<TResult>, Object>, Object, CancellationToken)Creates a continuation that executes when the target Task<TResult> completes.
Public methodSupported in .NET for Windows Store appsContinueWith(Action<Task<TResult>, Object>, Object, TaskContinuationOptions)Creates a continuation that executes when the target Task<TResult> completes.
Public methodSupported in .NET for Windows Store appsContinueWith(Action<Task<TResult>, Object>, Object, TaskScheduler)Creates a continuation that executes when the target Task<TResult> completes.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsContinueWith(Action<Task>, CancellationToken, TaskContinuationOptions, TaskScheduler)Creates a continuation that executes according to the specified TaskContinuationOptions. (Inherited from Task.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsContinueWith(Action<Task<TResult>>, CancellationToken, TaskContinuationOptions, TaskScheduler)Creates a continuation that executes according the condition specified in continuationOptions.
Public methodSupported in .NET for Windows Store appsContinueWith(Action<Task, Object>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler)Creates a continuation that executes when the target Task completes. (Inherited from Task.)
Public methodSupported in .NET for Windows Store appsContinueWith(Action<Task<TResult>, Object>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler)Creates a continuation that executes when the target Task<TResult> completes.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsContinueWith<TResult>(Func<Task, TResult>)Creates a continuation that executes asynchronously when the target Task completes. (Inherited from Task.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsContinueWith<TNewResult>(Func<Task<TResult>, TNewResult>)Creates a continuation that executes asynchronously when the target Task<TResult> completes.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsContinueWith<TResult>(Func<Task, TResult>, CancellationToken)Creates a continuation that executes asynchronously when the target Task completes. (Inherited from Task.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsContinueWith<TResult>(Func<Task, TResult>, TaskContinuationOptions)Creates a continuation that executes according to the condition specified in continuationOptions. (Inherited from Task.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsContinueWith<TResult>(Func<Task, TResult>, TaskScheduler)Creates a continuation that executes asynchronously when the target Task completes. (Inherited from Task.)
Public methodSupported in .NET for Windows Store appsContinueWith<TResult>(Func<Task, Object, TResult>, Object)Creates a continuation that executes when the target Task completes. (Inherited from Task.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsContinueWith<TNewResult>(Func<Task<TResult>, TNewResult>, CancellationToken)Creates a continuation that executes asynchronously when the target Task<TResult> completes.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsContinueWith<TNewResult>(Func<Task<TResult>, TNewResult>, TaskContinuationOptions)Creates a continuation that executes according the condition specified in continuationOptions.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsContinueWith<TNewResult>(Func<Task<TResult>, TNewResult>, TaskScheduler)Creates a continuation that executes asynchronously when the target Task<TResult> completes.
Public methodSupported in .NET for Windows Store appsContinueWith<TNewResult>(Func<Task<TResult>, Object, TNewResult>, Object)Creates a continuation that executes when the target Task<TResult> completes.
Public methodSupported in .NET for Windows Store appsContinueWith<TResult>(Func<Task, Object, TResult>, Object, CancellationToken)Creates a continuation that executes when the target Task completes. (Inherited from Task.)
Public methodSupported in .NET for Windows Store appsContinueWith<TResult>(Func<Task, Object, TResult>, Object, TaskContinuationOptions)Creates a continuation that executes when the target Task completes. (Inherited from Task.)
Public methodSupported in .NET for Windows Store appsContinueWith<TResult>(Func<Task, Object, TResult>, Object, TaskScheduler)Creates a continuation that executes when the target Task completes. (Inherited from Task.)
Public methodSupported in .NET for Windows Store appsContinueWith<TNewResult>(Func<Task<TResult>, Object, TNewResult>, Object, CancellationToken)Creates a continuation that executes when the target Task<TResult> completes.
Public methodSupported in .NET for Windows Store appsContinueWith<TNewResult>(Func<Task<TResult>, Object, TNewResult>, Object, TaskContinuationOptions)Creates a continuation that executes when the target Task<TResult> completes.
Public methodSupported in .NET for Windows Store appsContinueWith<TNewResult>(Func<Task<TResult>, Object, TNewResult>, Object, TaskScheduler)Creates a continuation that executes when the target Task<TResult> completes.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsContinueWith<TResult>(Func<Task, TResult>, CancellationToken, TaskContinuationOptions, TaskScheduler)Creates a continuation that executes according to the condition specified in continuationOptions. (Inherited from Task.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsContinueWith<TNewResult>(Func<Task<TResult>, TNewResult>, CancellationToken, TaskContinuationOptions, TaskScheduler)Creates a continuation that executes according the condition specified in continuationOptions.
Public methodSupported in .NET for Windows Store appsContinueWith<TResult>(Func<Task, Object, TResult>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler)Creates a continuation that executes when the target Task completes. (Inherited from Task.)
Public methodSupported in .NET for Windows Store appsContinueWith<TNewResult>(Func<Task<TResult>, Object, TNewResult>, Object, CancellationToken, TaskContinuationOptions, TaskScheduler)Creates a continuation that executes when the target Task<TResult> completes.
Public methodDispose()Releases all resources used by the current instance of the Task class. (Inherited from Task.)
Protected methodDispose(Boolean)Disposes the Task, releasing all of its unmanaged resources. (Inherited from Task.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported in .NET for Windows Store appsGetAwaiterGets an awaiter used to await this Task<TResult>.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsRunSynchronously()Runs the Task synchronously on the current TaskScheduler. (Inherited from Task.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsRunSynchronously(TaskScheduler)Runs the Task synchronously on the TaskScheduler provided. (Inherited from Task.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsStart()Starts the Task, scheduling it for execution to the current TaskScheduler. (Inherited from Task.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsStart(TaskScheduler)Starts the Task, scheduling it for execution to the specified TaskScheduler. (Inherited from Task.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsWait()Waits for the Task to complete execution. (Inherited from Task.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsWait(CancellationToken)Waits for the cancellable Task to complete execution. (Inherited from Task.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsWait(Int32)Waits for the Task to complete execution within a specified number of milliseconds. (Inherited from Task.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsWait(TimeSpan)Waits for the Task to complete execution within a specified time interval. (Inherited from Task.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsWait(Int32, CancellationToken)Waits for the cancellable Task to complete execution. (Inherited from Task.)
Top

  NameDescription
Public Extension MethodSupported in .NET for Windows Store appsAsAsyncActionReturns a Windows Runtime asynchronous action that represents a started task. (Defined by WindowsRuntimeSystemExtensions.)
Public Extension MethodSupported in .NET for Windows Store appsAsAsyncOperation<TResult>Returns a Windows Runtime asynchronous operation that represents a started task that returns a result. (Defined by WindowsRuntimeSystemExtensions.)
Public Extension MethodDispatcherOperationWait()Overloaded. Waits indefinitely for the underlying DispatcherOperation to complete. (Defined by TaskExtensions.)
Public Extension MethodDispatcherOperationWait(TimeSpan)Overloaded. Waits for the specified amount of time for the underlying DispatcherOperation to complete. (Defined by TaskExtensions.)
Public Extension MethodIsDispatcherOperationTaskReturns a value that indicates whether this Task is associated with a DispatcherOperation. (Defined by TaskExtensions.)
Top

  NameDescription
Explicit interface implemetationPrivate propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsIAsyncResult.AsyncWaitHandleGets a WaitHandle that can be used to wait for the task to complete. (Inherited from Task.)
Explicit interface implemetationPrivate propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsIAsyncResult.CompletedSynchronouslyGets an indication of whether the operation completed synchronously. (Inherited from Task.)
Top

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 Task Parallelism (Task Parallel Library).

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.

NoteNote

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: Synchronization | ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

.NET Framework

Supported in: 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

All members of Task<TResult>, except for Dispose, are thread-safe and may be used from multiple threads concurrently.

Show:
© 2014 Microsoft