Edit

Share via


TaskCompletionSource Class

Definition

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

public ref class TaskCompletionSource
public class TaskCompletionSource
type TaskCompletionSource = class
Public Class TaskCompletionSource
Inheritance
TaskCompletionSource

Remarks

It is often the case that a Task is desired to represent another asynchronous operation. TaskCompletionSource 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. All members of TaskCompletionSource are thread-safe and may be used from multiple threads concurrently.

Constructors

TaskCompletionSource()

Creates a TaskCompletionSource.

TaskCompletionSource(Object)

Creates a TaskCompletionSource with the specified state.

TaskCompletionSource(Object, TaskCreationOptions)

Creates a TaskCompletionSource with the specified state and options.

TaskCompletionSource(TaskCreationOptions)

Creates a TaskCompletionSource with the specified options.

Properties

Task

Gets the Task created by this TaskCompletionSource.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(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 into the Canceled state.

SetCanceled(CancellationToken)

Transitions the underlying Task into the Canceled state using the specified token.

SetException(Exception)

Transitions the underlying Task into the Faulted state.

SetException(IEnumerable<Exception>)

Transitions the underlying Task into the Faulted state.

SetFromTask(Task)

Transitions the underlying Task<TResult> into the same completion state as the specified completedTask.

SetResult()

Transitions the underlying Task into the RanToCompletion state.

ToString()

Returns a string that represents the current object.

(Inherited from Object)
TrySetCanceled()

Attempts to transition the underlying Task into the Canceled state.

TrySetCanceled(CancellationToken)

Attempts to transition the underlying Task into the Canceled state.

TrySetException(Exception)

Attempts to transition the underlying Task into the Faulted state.

TrySetException(IEnumerable<Exception>)

Attempts to transition the underlying Task into the Faulted state.

TrySetFromTask(Task)

Attempts to transition the underlying Task<TResult> into the same completion state as the specified completedTask.

TrySetResult()

Attempts to transition the underlying Task into the RanToCompletion state.

Applies to