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)
繼承
實作

備註

AValueTask執行個體可能會等候或轉換成Task使用AsTaskA ValueTask instance may either be awaited or converted to a Task using AsTask. AValueTask執行個體可能只有一次,等候,而且取用者可能不會呼叫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.

AValueTask是一種結構可以包裝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 物件是否等於目前的 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.

適用於