Export (0) Print
Expand All

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
Assemblies:   System.Threading.Tasks (in System.Threading.Tasks.dll)
  mscorlib (in mscorlib.dll)

type TaskCompletionSource<'TResult> =  class end

Type Parameters

TResult

The type of the result value assocatied with this TaskCompletionSource(TResult).

The TaskCompletionSource(TResult) type exposes the following members.

  NameDescription
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsTaskCompletionSource(TResult)()Creates a TaskCompletionSource(TResult).
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsTaskCompletionSource(TResult)(Object)Creates a TaskCompletionSource(TResult) with the specified state.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsTaskCompletionSource(TResult)(TaskCreationOptions)Creates a TaskCompletionSource(TResult) with the specified options.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsTaskCompletionSource(TResult)(Object, TaskCreationOptions)Creates a TaskCompletionSource(TResult) with the specified state and options.
Top

  NameDescription
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsTaskGets the Task(TResult) created by this TaskCompletionSource(TResult).
Top

  NameDescription
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 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 appsSetCanceledTransitions the underlying Task(TResult) into the Canceled state.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsSetException(IEnumerable(Exception))Transitions the underlying Task(TResult) into the Faulted state.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsSetException(Exception)Transitions the underlying Task(TResult) into the Faulted state.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsSetResultTransitions the underlying Task(TResult) into the RanToCompletion state.
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 appsTrySetCanceled()Attempts to transition the underlying Task(TResult) into the Canceled state.
Public methodTrySetCanceled(CancellationToken)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsTrySetException(IEnumerable(Exception))Attempts to transition the underlying Task(TResult) into the Faulted state.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsTrySetException(Exception)Attempts to transition the underlying Task(TResult) into the Faulted state.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsTrySetResultAttempts 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 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.

.NET Framework

Supported in: 4.6, 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

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: 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 TaskCompletionSource(TResult) are thread-safe and may be used from multiple threads concurrently.

Show:
© 2014 Microsoft