ValueTask ValueTask ValueTask ValueTask Struct

定义

提供异步操作的可等待结果。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)
继承
实现

注解

实例可以等待或Task使用AsTask转换为。 ValueTaskA ValueTask instance may either be awaited or converted to a Task using AsTask. 实例仅可等待一次, 并且在实例完成之前, 使用者可能不会调用GetAwaiter()ValueTaskA ValueTask instance may only be awaited once, and consumers may not call GetAwaiter() until the instance has completed. 如果这些限制不可接受, 请ValueTask Task通过调用AsTask将转换为。If 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.
  • 多次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.

是可包装TaskIValueTaskSource实例的结构。 ValueTaskA ValueTask is a structure that can wrap either a Task or a IValueTaskSource instance. 如果返回ValueTask一个从异步IValueTaskSource方法包装实例的, 则会启用高吞吐量应用程序, 以避免使用可重用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.

使用而不是会Task引入一些开销。 ValueTaskUsing 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才应使用, 而Task不是。Only if performance analysis proves it worthwhile should a ValueTask be used instead of a Task. 此属性应该用于在方法Task返回成功完成的情况下, 手动返回已成功完成的单一实例。 Task.CompletedTaskThe 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(Task) ValueTask(Task) ValueTask(Task) ValueTask(Task)

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

ValueTask(IValueTaskSource, Int16) ValueTask(IValueTaskSource, Int16) ValueTask(IValueTaskSource, Int16) ValueTask(IValueTaskSource, Int16)

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

属性

IsCanceled IsCanceled IsCanceled IsCanceled

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

IsCompleted IsCompleted IsCompleted IsCompleted

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

IsCompletedSuccessfully IsCompletedSuccessfully IsCompletedSuccessfully IsCompletedSuccessfully

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

IsFaulted IsFaulted IsFaulted IsFaulted

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

方法

AsTask() AsTask() AsTask() AsTask()

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

ConfigureAwait(Boolean) ConfigureAwait(Boolean) ConfigureAwait(Boolean) ConfigureAwait(Boolean)

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

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

Equals(ValueTask) Equals(ValueTask) Equals(ValueTask) Equals(ValueTask)

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

GetAwaiter() GetAwaiter() GetAwaiter() GetAwaiter()

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

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

Preserve() Preserve() Preserve() Preserve()

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

操作员

Equality(ValueTask, ValueTask) Equality(ValueTask, ValueTask) Equality(ValueTask, ValueTask) Equality(ValueTask, ValueTask)

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

Inequality(ValueTask, ValueTask) Inequality(ValueTask, ValueTask) Inequality(ValueTask, ValueTask) Inequality(ValueTask, ValueTask)

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

适用于