TaskCompletionSource 类

定义

表示未绑定到委托的 Task 的制造者方,并通过 Task 属性提供对使用者方的访问。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
继承
TaskCompletionSource

注解

通常情况下, Task 需要表示另一个异步操作。It is often the case that a Task is desired to represent another asynchronous operation. TaskCompletionSource 提供此目的。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. 但是,与大多数任务不同,由创建的任务的状态由 TaskCompletionSource 中的方法显式控制 TaskCompletionSourceHowever, unlike most tasks, the state of a task created by a TaskCompletionSource is controlled explicitly by the methods on TaskCompletionSource. 这使得外部异步操作能够传播到基础 TaskThis enables the completion of the external asynchronous operation to be propagated to the underlying Task. 分隔还可确保使用者不能在不访问相应的的情况下转换状态 TaskCompletionSourceThe separation also ensures that consumers are not able to transition the state without access to the corresponding TaskCompletionSource. 的所有成员 TaskCompletionSource 都是线程安全的,可同时从多个线程使用。All members of TaskCompletionSource are thread-safe and may be used from multiple threads concurrently.

构造函数

TaskCompletionSource()

创建一个 TaskCompletionSourceCreates a TaskCompletionSource.

TaskCompletionSource(Object)

使用指定的状态创建一个 TaskCompletionSourceCreates a TaskCompletionSource with the specified state.

TaskCompletionSource(Object, TaskCreationOptions)

使用指定的状态和选项创建一个 TaskCompletionSourceCreates a TaskCompletionSource with the specified state and options.

TaskCompletionSource(TaskCreationOptions)

使用指定的选项创建一个 TaskCompletionSourceCreates a TaskCompletionSource with the specified options.

属性

Task

获取由此 Task 创建的 TaskCompletionSourceGets the Task created by this TaskCompletionSource.

方法

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
SetCanceled()

将基础 Task 转换为 Canceled 状态。Transitions the underlying Task into the Canceled state.

SetCanceled(CancellationToken)

使用指定的标记将基础 Task 转换为 Canceled 状态。Transitions the underlying Task into the Canceled state using the specified token.

SetException(Exception)

将基础 Task 转换为 Faulted 状态。Transitions the underlying Task into the Faulted state.

SetException(IEnumerable<Exception>)

将基础 Task 转换为 Faulted 状态。Transitions the underlying Task into the Faulted state.

SetResult()

将基础 Task 转换为 RanToCompletion 状态。Transitions the underlying Task into the RanToCompletion state.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)
TrySetCanceled()

尝试将基础 Task 转换为 Canceled 状态。Attempts to transition the underlying Task into the Canceled state.

TrySetCanceled(CancellationToken)

尝试将基础 Task 转换为 Canceled 状态。Attempts to transition the underlying Task into the Canceled state.

TrySetException(Exception)

尝试将基础 Task 转换为 Faulted 状态。Attempts to transition the underlying Task into the Faulted state.

TrySetException(IEnumerable<Exception>)

尝试将基础 Task 转换为 Faulted 状态。Attempts to transition the underlying Task into the Faulted state.

TrySetResult()

尝试将基础 Task 转换为 RanToCompletion 状态。Attempts to transition the underlying Task into the RanToCompletion state.

适用于