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 的实例可以使用 AsTask等待或转换为 TaskA 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. 如果这些限制不可接受,请通过调用 AsTaskValueTask 转换为 TaskIf these limitations are unacceptable, convert the ValueTask to a Task by calling AsTask.

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

  • 等待实例多次。Awaiting the instance multiple times.
  • 多次调用 AsTaskCalling 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 是一种可以包装 TaskIValueTaskSource 实例的结构。A ValueTask is a structure that can wrap either a Task or a IValueTaskSource instance. 返回一个通过异步方法包装 IValueTaskSource 实例的 ValueTask,这使得高吞吐量应用程序可以通过使用可重用的 IValueTaskSource 对象池来避免分配。Returning 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 属性应该用于在返回 Task 的方法同步成功完成的情况下,手动返回已成功完成的单一实例。The 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.

备注

支持从C# 7.0 开始使用 ValueTask 类型,且 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)

使用提供的表示操作的 ValueTask 对象初始化 IValueTaskSource 类的新实例。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.

属性

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()

检索表示此 TaskValueTask 对象。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.

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.

适用于