TaskCompletionSource<TResult> Class

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

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

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

generic<typename TResult>
[HostProtectionAttribute(SecurityAction::LinkDemand, Synchronization = true, 
	ExternalThreading = true)]
public ref class TaskCompletionSource 

Type Parameters

TResult

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

NameDescription
System_CAPS_pubmethodTaskCompletionSource<TResult>()

Creates a TaskCompletionSource<TResult>.

System_CAPS_pubmethodTaskCompletionSource<TResult>(Object^)

Creates a TaskCompletionSource<TResult> with the specified state.

System_CAPS_pubmethodTaskCompletionSource<TResult>(Object^, TaskCreationOptions)

Creates a TaskCompletionSource<TResult> with the specified state and options.

System_CAPS_pubmethodTaskCompletionSource<TResult>(TaskCreationOptions)

Creates a TaskCompletionSource<TResult> with the specified options.

NameDescription
System_CAPS_pubpropertyTask

Gets the Task<TResult> created by this TaskCompletionSource<TResult>.

NameDescription
System_CAPS_pubmethodEquals(Object^)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodSetCanceled()

Transitions the underlying Task<TResult> into the Canceled state.

System_CAPS_pubmethodSetException(Exception^)

Transitions the underlying Task<TResult> into the Faulted state and binds it to a specified exception.

System_CAPS_pubmethodSetException(IEnumerable<Exception^>^)

Transitions the underlying Task<TResult> into the TaskStatus::Faulted state and binds a collection of exception objects to it.

System_CAPS_pubmethodSetResult(TResult)

Transitions the underlying Task<TResult> into the RanToCompletion state.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

System_CAPS_pubmethodTrySetCanceled()

Attempts to transition the underlying Task<TResult> into the Canceled state.

System_CAPS_pubmethodTrySetCanceled(CancellationToken)

Attempts to transition the underlying Task<TResult> into the TaskStatus::Canceled state and enables a cancellation token to be stored in the canceled task.

System_CAPS_pubmethodTrySetException(Exception^)

Attempts to transition the underlying Task<TResult> into the Faulted state and binds it to a specified exception.

System_CAPS_pubmethodTrySetException(IEnumerable<Exception^>^)

Attempts to transition the underlying Task<TResult> into the Faulted state and binds a collection of exception objects to it.

System_CAPS_pubmethodTrySetResult(TResult)

Attempts to transition the underlying Task<TResult> into the RanToCompletion state.

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 the entry The Nature of TaskCompletionSource<TResult> in the Parallel Programming with .NET blog.

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

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

No code example is currently available or this language may not be supported.

Universal Windows Platform
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 TaskCompletionSource<TResult> are thread-safe and may be used from multiple threads concurrently.

Return to top
Show: