Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

TaskCompletionSource<TResult> Class

Represents the producer side of a Task<TResult> unbound to a delegate, providing access to the consumer side through the Task property.

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

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

public class TaskCompletionSource<TResult>

Type Parameters

TResult

The type of the result value assocatied with this TaskCompletionSource<TResult>.

The TaskCompletionSource<TResult> type exposes the following members.

  NameDescription
Public methodTaskCompletionSource<TResult>()Creates a TaskCompletionSource<TResult>.
Public methodTaskCompletionSource<TResult>(Object)Creates a TaskCompletionSource<TResult> with the specified state.
Public methodTaskCompletionSource<TResult>(TaskCreationOptions)Creates a TaskCompletionSource<TResult> with the specified options.
Public methodTaskCompletionSource<TResult>(Object, TaskCreationOptions)Creates a TaskCompletionSource<TResult> with the specified state and options.
Top

  NameDescription
Public propertyTaskGets the Task<TResult> created by this TaskCompletionSource<TResult>.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSetCanceledTransitions the underlying Task<TResult> into the Canceled state.
Public methodSetException(IEnumerable<Exception>)Transitions the underlying Task<TResult> into the Faulted state.
Public methodSetException(Exception)Transitions the underlying Task<TResult> into the Faulted state.
Public methodSetResultTransitions the underlying Task<TResult> into the RanToCompletion state.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodTrySetCanceledAttempts to transition the underlying Task<TResult> into the Canceled state.
Public methodTrySetException(IEnumerable<Exception>)Attempts to transition the underlying Task<TResult> into the Faulted state.
Public methodTrySetException(Exception)Attempts to transition the underlying Task<TResult> into the Faulted state.
Public methodTrySetResultAttempts to transition the underlying Task<TResult> into the RanToCompletion state.
Top

In many scenarios, it is useful to enable a Task<TResult> to represent an external asynchronous operation. TaskCompletionSource<TResult> is provided for this purpose. It enables the creation of a task that can be handed out to consumers, and those consumers can use the members of the task as they would any other. However, unlike most tasks, the state of a task created by a TaskCompletionSource is controlled explicitly by the methods on TaskCompletionSource. This enables the completion of the external asynchronous operation to be propagated to the underlying Task. The separation also ensures that consumers are not able to transition the state without access to the corresponding TaskCompletionSource.

For more information, see TPL and Traditional .NET Asynchronous Programming

The Parallel Extensions samples also contain examples of how to use TaskCompletionSource<TResult>.

The following example shows how to use a TaskCompletionSource<TResult>:

Silverlight

Supported in: 5

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

All members of TaskCompletionSource<TResult> are thread-safe and may be used from multiple threads concurrently.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft