ValueTask 结构

定义

提供异步操作的可等待结果。Provides an awaitable result of an asynchronous operation.

public value class ValueTask : IEquatable<System::Threading::Tasks::ValueTask>
public struct ValueTask : IEquatable<System.Threading.Tasks.ValueTask>
type ValueTask = struct
Public Structure ValueTask
Implements IEquatable(Of ValueTask)
继承
ValueTask
实现

注解

ValueTask实例可以等待或使用转换为 Task AsTaskA ValueTask instance may either be awaited or converted to a Task using AsTask. ValueTask实例仅可等待一次,并且 GetAwaiter() 在实例完成之前,使用者可能不会调用。A ValueTask instance may only be awaited once, and consumers may not call GetAwaiter() until the instance has completed. 如果这些限制不可接受,请 ValueTask Task 通过调用将转换为 AsTaskIf these limitations are unacceptable, convert the ValueTask to a Task by calling AsTask.

永远不应对实例执行以下操作 ValueTaskThe following operations should never be performed on a ValueTask instance:

  • 等待实例多次。Awaiting the instance multiple times.
  • AsTask多次调用。Calling AsTask multiple times.
  • 使用多种方法来使用此实例。Using more than one of these techniques to consume the instance.

如果执行上述任一操作,则结果是不确定的。If you do any of the above, the results are undefined.

ValueTask是可包装 Task 或实例的结构 IValueTaskSourceA ValueTask is a structure that can wrap either a Task or a IValueTaskSource instance. 如果返回一个 ValueTask 从异步方法包装实例的,则会 IValueTaskSource 启用高吞吐量应用程序,以避免使用可重用对象池进行分配 IValueTaskSourceReturning a ValueTask that wraps a IValueTaskSource instance from an asynchronous method enables high-throughput applications to avoid allocations by using a pool of reusable IValueTaskSource objects. 有关详细信息,请参阅 了解原因和 ValueTask的内容。For more information, see Understanding the Whys, Whats, and Whens of ValueTask.

使用 ValueTask 而不是会 Task 引入一些开销。Using a ValueTask instead of a Task introduces some overhead. 由于 ValueTask 是具有多个字段的结构,因此从方法返回它将导致复制更多的数据,并返回一个 Task 引用。Because ValueTask is a structure with multiple fields, returning it from the method results in copying more data compared to returning a single Task reference. 因此,不返回结果的任何异步方法的默认选择都应该是返回 TaskAs such, the default choice for any asynchronous method that does not return a result should be to return a Task. 仅当性能分析证明需要使用时,才应 ValueTask 使用,而不是 TaskOnly if performance analysis proves it worthwhile should a ValueTask be used instead of a Task. Task.CompletedTask此属性应该用于在方法返回成功完成的情况下,手动返回已成功完成的单一实例 TaskThe Task.CompletedTask property should be used to hand back a successfully completed singleton in the case where a method returning a Task completes synchronously and successfully.

备注

支持使用类型, ValueTask 从 c # 7.0 开始,不受 Visual Basic 的任何版本支持。The use of the ValueTask type is supported starting with C# 7.0 and is not supported by any version of Visual Basic.

构造函数

ValueTask(IValueTaskSource, Int16)

使用提供的表示操作的 IValueTaskSource 对象初始化 ValueTask 类的新实例。Initializes a new instance of the ValueTask class using the supplied IValueTaskSource object that represents the operation.

ValueTask(Task)

使用提供的表示操作的任务初始化 ValueTask 类的新实例。Initializes a new instance of the ValueTask class using the supplied task that represents the operation.

属性

CompletedTask

获取一个已成功完成的任务。Gets a task that has already completed successfully.

IsCanceled

获取指示此对象是否表示取消的操作的值。Gets a value that indicates whether this object represents a canceled operation.

IsCompleted

获取指示此对象是否表示完成的操作的值。Gets a value that indicates whether this object represents a completed operation.

IsCompletedSuccessfully

获取指示此对象是否表示成功完成的操作的值。Gets a value that indicates whether this object represents a successfully completed operation.

IsFaulted

获取指示此对象是否表示失败的操作的值。Gets a value that indicates whether this object represents a failed operation.

方法

AsTask()

检索表示此 ValueTaskTask 对象。Retrieves a Task object that represents this ValueTask.

ConfigureAwait(Boolean)

为此值配置一个 awaiter。Configures an awaiter for this value.

Equals(Object)

确定指定对象是否与当前的 ValueTask 实例相等。Determines whether the specified object is equal to the current ValueTask instance.

Equals(ValueTask)

确定指定的 ValueTask 对象是否等同于当前的 ValueTaskDetermines whether the specified ValueTask object is equal to the current ValueTask object.

FromCanceled(CancellationToken)

创建 ValueTask,它因指定的取消标记进行的取消操作而完成。Creates a ValueTask that has completed due to cancellation with the specified cancellation token.

FromCanceled<TResult>(CancellationToken)

创建 ValueTask<TResult>,它因指定的取消标记进行的取消操作而完成。Creates a ValueTask<TResult> that has completed due to cancellation with the specified cancellation token.

FromException(Exception)

创建 ValueTask,它是以指定的异常来完成的。Creates a ValueTask that has completed with the specified exception.

FromException<TResult>(Exception)

创建 ValueTask<TResult>,它是以指定的异常来完成的。Creates a ValueTask<TResult> that has completed with the specified exception.

FromResult<TResult>(TResult)

创建指定结果的、成功完成的 ValueTask<TResult>Creates a ValueTask<TResult> that's completed successfully with the specified result.

GetAwaiter()

为此值创建一个 awaiter。Creates an awaiter for this value.

GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

Preserve()

获取将来随时可用的 ValueTaskGets a ValueTask that may be used at any point in the future.

运算符

Equality(ValueTask, ValueTask)

比较两个 ValueTask 值以判断是否相等。Compares two ValueTask values for equality.

Inequality(ValueTask, ValueTask)

确定两个 ValueTask 值是否不相等。Determines whether two ValueTask values are unequal.

适用于