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)
继承
实现

注解

一个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. 如果不可接受的这些限制,将转换ValueTaskTask通过调用AsTaskIf 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.

一个ValueTask是一个结构,可以是包装TaskIValueTaskSource实例。A 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. 有关详细信息,请参阅了解原因,新增功能,以及何时具有的 ValueTaskFor 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.

备注

利用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(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.

适用于