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 | |
---|---|---|
TaskCompletionSource<TResult>() | Creates a TaskCompletionSource<TResult>. | |
TaskCompletionSource<TResult>(Object) | Creates a TaskCompletionSource<TResult> with the specified state. | |
TaskCompletionSource<TResult>(TaskCreationOptions) | Creates a TaskCompletionSource<TResult> with the specified options. | |
TaskCompletionSource<TResult>(Object, TaskCreationOptions) | Creates a TaskCompletionSource<TResult> with the specified state and options. |
Top
Properties
Name | Description | |
---|---|---|
Task | Gets the Task<TResult> created by this TaskCompletionSource<TResult>. |
Top
Methods
Name | Description | |
---|---|---|
Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) | |
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.) | |
GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
SetCanceled | Transitions the underlying Task<TResult> into the Canceled state. | |
SetException(IEnumerable<Exception>) | Transitions the underlying Task<TResult> into the Faulted state. | |
SetException(Exception) | Transitions the underlying Task<TResult> into the Faulted state. | |
SetResult | Transitions the underlying Task<TResult> into the RanToCompletion state. | |
ToString | Returns a string that represents the current object. (Inherited from Object.) | |
TrySetCanceled | Attempts to transition the underlying Task<TResult> into the Canceled state. | |
TrySetException(IEnumerable<Exception>) | Attempts to transition the underlying Task<TResult> into the Faulted state. | |
TrySetException(Exception) | Attempts to transition the underlying Task<TResult> into the Faulted state. | |
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.
See Also