TaskCompletionSource<TResult> Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Inheritance Hierarchy

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

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

Syntax

'Declaration
Public Class TaskCompletionSource(Of TResult)
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.

Constructors

  Name Description
Public method TaskCompletionSource<TResult>() Creates a TaskCompletionSource<TResult>.
Public method TaskCompletionSource<TResult>(Object) Creates a TaskCompletionSource<TResult> with the specified state.
Public method TaskCompletionSource<TResult>(TaskCreationOptions) Creates a TaskCompletionSource<TResult> with the specified options.
Public method TaskCompletionSource<TResult>(Object, TaskCreationOptions) Creates a TaskCompletionSource<TResult> with the specified state and options.

Top

Properties

  Name Description
Public property Task Gets the Task<TResult> created by this TaskCompletionSource<TResult>.

Top

Methods

  Name Description
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method 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.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method SetCanceled Transitions the underlying Task<TResult> into the Canceled state.
Public method SetException(IEnumerable<Exception>) Transitions the underlying Task<TResult> into the Faulted state.
Public method SetException(Exception) Transitions the underlying Task<TResult> into the Faulted state.
Public method SetResult Transitions the underlying Task<TResult> into the RanToCompletion state.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method TrySetCanceled Attempts to transition the underlying Task<TResult> into the Canceled state.
Public method TrySetException(IEnumerable<Exception>) Attempts to transition the underlying Task<TResult> into the Faulted state.
Public method TrySetException(Exception) Attempts to transition the underlying Task<TResult> into the Faulted state.
Public method TrySetResult Attempts to transition the underlying Task<TResult> into the RanToCompletion state.

Top

Remarks

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>.

Examples

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

Version Information

Silverlight

Supported in: 5

Platforms

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

Thread Safety

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