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 AsTask無法接受這些限制, 請呼叫將轉換成。 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.
  • 多次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. 傳回從非同步方法IValueTaskSource包裝實例的IValueTaskSource ,可讓高輸送量應用程式使用可重複使用物件的集區來避免配置。ValueTaskReturning 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. 因此, 不會傳回結果之任何非同步方法的預設選擇, 應該會Task傳回。As 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 物件是否等於目前的 ValueTask 物件。Determines 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.

適用於