ValueTask<TResult> Yapı
Tanım
Task<TResult>Yalnızca biri kullanılan bir ve olan bir değer türü sağlar TResult .Provides a value type that wraps a Task<TResult> and a TResult, only one of which is used.
generic <typename TResult>
public value class ValueTask : IEquatable<System::Threading::Tasks::ValueTask<TResult>>
public struct ValueTask<TResult> : IEquatable<System.Threading.Tasks.ValueTask<TResult>>
type ValueTask<'Result> = struct
Public Structure ValueTask(Of TResult)
Implements IEquatable(Of ValueTask(Of TResult))
Tür Parametreleri
- TResult
Sonuç.The result.
- Devralma
- Uygulamalar
-
IEquatable<ValueTask<TResult>>
Açıklamalar
Bir ValueTask<TResult> örnek bir kullanımı beklemiş veya bir using öğesine dönüştürülebilir Task<TResult> olabilir AsTask .A ValueTask<TResult> instance may either be awaited or converted to a Task<TResult> using AsTask. ValueTask<TResult>Örnek yalnızca bir kez beklemiş olabilir ve örnek tamamlanana kadar tüketiciler okunmayabilir Result .A ValueTask<TResult> instance may only be awaited once, and consumers may not read Result until the instance has completed. Bu sınırlamalar kabul edilemez ise, çağırarak öğesini ValueTask<TResult> öğesine dönüştürün Task<TResult> AsTask .If these limitations are unacceptable, convert the ValueTask<TResult> to a Task<TResult> by calling AsTask.
Aşağıdaki işlemler bir örnek üzerinde hiçbir şekilde gerçekleştirilmemelidir ValueTask<TResult> :The following operations should never be performed on a ValueTask<TResult> instance:
- Örnek birden çok kez bekleniyor.Awaiting the instance multiple times.
- AsTaskBirden çok kez çağırma.Calling AsTask multiple times.
.ResultYa da.GetAwaiter().GetResult()işlem henüz tamamlanmamış ya da birden çok kez kullanılıyor.Using.Resultor.GetAwaiter().GetResult()when the operation hasn't yet completed, or using them multiple times.- Örneği tüketmek için bu tekniklerden daha fazlasını kullanma.Using more than one of these techniques to consume the instance.
Yukarıdakilerden herhangi birini yaparsanız sonuçlar tanımsızdır.If you do any of the above, the results are undefined.
Bir yöntem, işleminin sonucunun eşzamanlı olarak kullanılabilir olacağı büyük ihtimalle ve çağrılması beklendiğinde, bu değer türünün bir örneğini döndürebilir ve bu durum, her bir çağrı için yeni bir ayırma maliyeti mümkün olduğunda sıklıkla çağrılabilir Task<TResult> .A method may return an instance of this value type when it's likely that the result of its operation will be available synchronously, and when it's expected to be invoked so frequently that the cost of allocating a new Task<TResult> for each call will be prohibitive.
Bir yerine bir kullanmanın avantajları vardır ValueTask<TResult> Task<TResult> .There are tradeoffs to using a ValueTask<TResult> instead of a Task<TResult>. Örneğin, bir, ValueTask<TResult> başarılı sonucun zaman uyumlu olarak kullanılabildiği durumda bir ayırmayı önlemenize yardımcı olabilir, ancak bir Task<TResult> başvuru türü olarak tek bir alan olmak üzere birden çok alan da içerir.For example, while a ValueTask<TResult> can help avoid an allocation in the case where the successful result is available synchronously, it also contains multiple fields, whereas a Task<TResult> as a reference type is a single field. Bu, bir yöntemden döndürme, ValueTask<TResult> daha fazla veri kopyalamaya neden olacağı anlamına gelir.This means that returning a ValueTask<TResult> from a method results in copying more data. Ayrıca, bir zaman uyumsuz yöntem içinde bir döndüren bir yöntem ValueTask<TResult> , tek bir başvuru yerine birden çok alan içeren bir yapı depolaması gerektiğinden, zaman uyumsuz yöntem için durum makinesinin daha büyük olacağı anlamına gelir.It also means, that if a method that returns a ValueTask<TResult> is awaited within an async method, the state machine for that async method will be larger, because it must store a struct containing multiple fields instead of a single reference.
Await kullanarak zaman uyumsuz bir işlemin sonucunu tükettikten başka kullanımlar için, ValueTask<TResult> daha fazla ayırma gerektiren daha karmaşık bir programlama modeline yol açabilir.For uses other than consuming the result of an asynchronous operation using await, ValueTask<TResult> can lead to a more convoluted programming model that requires more allocations. Örneğin, Task<TResult> ortak bir sonuç olarak, önbelleğe alınmış bir görevle birlikte döndüren bir yöntemi düşünün ValueTask<TResult> .For example, consider a method that could return either a Task<TResult> with a cached task as a common result or a ValueTask<TResult>. Sonucun tüketicisi, ya da gibi bir yöntemde kullanmak isterse, ilk olarak bir Task<TResult> WhenAll WhenAny ValueTask<TResult> Task<TResult> AsTask önbelleğe alınmış Task<TResult> ilk yerde kullanılmışsa, bu, önce bir using öğesine dönüştürülmelidir.If the consumer of the result wants to use it as a Task<TResult> in a method like WhenAll or WhenAny, the ValueTask<TResult> must first be converted to a Task<TResult> using AsTask, leading to an allocation that would have been avoided if a cached Task<TResult> had been used in the first place.
Bu nedenle, herhangi bir zaman uyumsuz yöntem için varsayılan seçenek Task veya döndürmelidir Task<TResult> .As such, the default choice for any asynchronous method should be to return a Task or Task<TResult>. Yalnızca performans analizinin ValueTask<TResult> bir yerine kullanılması gerektiğini kanıtlarken Task<TResult> .Only if performance analysis proves it worthwhile should a ValueTask<TResult> be used instead of a Task<TResult>. Genel olmayan sürümü ValueTask çoğu senaryo için önerilmez.The non generic version of ValueTask is not recommended for most scenarios. CompletedTaskÖzelliği, Task zaman uyumlu olarak ve başarılı bir şekilde bir şekilde döndürülen bir yöntemin başarıyla tamamlandığını geri almak için kullanılmalıdır.The 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.
Not
ValueTask<TResult>Türü kullanımı C# 7,0 ile başlayarak desteklenir ve Visual Basic herhangi bir sürümü tarafından desteklenmez.The use of the ValueTask<TResult> type is supported starting with C# 7.0, and is not supported by any version of Visual Basic.
Not
Parametresiz oluşturucusu veya default(ValueTask<TResult>) sözdizimi (sıfır ile başlatılmış bir yapı) ile oluşturulan bir örnek, zaman uyumlu olarak bir işlemi sonucuyla birlikte, başarıyla tamamlandığını temsil eder default(TResult) .An instance created with the parameterless constructor or by the default(ValueTask<TResult>) syntax (a zero-initialized structure) represents a synchronously, successfully completed operation with a result of default(TResult).
Oluşturucular
| ValueTask<TResult>(IValueTaskSource<TResult>, Int16) |
ValueTask<TResult>İşlemi temsil eden bir nesne ile sınıfının yeni bir örneğini başlatır IValueTaskSource<TResult> .Initializes a new instance of the ValueTask<TResult> class with a IValueTaskSource<TResult> object that represents the operation. |
| ValueTask<TResult>(Task<TResult>) |
ValueTask<TResult>İşlemi temsil eden sağlanan görevi kullanarak sınıfının yeni bir örneğini başlatır.Initializes a new instance of the ValueTask<TResult> class using the supplied task that represents the operation. |
| ValueTask<TResult>(TResult) |
ValueTask<TResult>Başarılı bir işlemin sağlanan sonucunu kullanarak sınıfının yeni bir örneğini başlatır.Initializes a new instance of the ValueTask<TResult> class using the supplied result of a successful operation. |
Özellikler
| IsCanceled |
Bu nesnenin iptal edilmiş bir işlemi temsil edip etmediğini gösteren bir değer alır.Gets a value that indicates whether this object represents a canceled operation. |
| IsCompleted |
Bu nesnenin tamamlanmış bir işlemi temsil edip etmediğini gösteren bir değer alır.Gets a value that indicates whether this object represents a completed operation. |
| IsCompletedSuccessfully |
Bu nesnenin başarıyla tamamlanan bir işlemi temsil edip etmediğini gösteren bir değer alır.Gets a value that indicates whether this object represents a successfully completed operation. |
| IsFaulted |
Bu nesnenin başarısız bir işlemi temsil edip etmediğini gösteren bir değer alır.Gets a value that indicates whether this object represents a failed operation. |
| Result |
Sonucu alır.Gets the result. |
Yöntemler
| AsTask() |
Task<TResult>Bunu temsil eden bir nesne alır ValueTask<TResult> .Retrieves a Task<TResult> object that represents this ValueTask<TResult>. |
| ConfigureAwait(Boolean) |
Bu değer için bir awaiter yapılandırır.Configures an awaiter for this value. |
| CreateAsyncMethodBuilder() |
Zaman uyumsuz bir yöntemle kullanılmak üzere bir yöntem Oluşturucu oluşturur.Creates a method builder for use with an async method. |
| Equals(Object) |
Belirtilen nesnenin geçerli nesneye eşit olup olmadığını belirler.Determines whether the specified object is equal to the current object. |
| Equals(ValueTask<TResult>) |
Belirtilen ValueTask<TResult> nesnenin geçerli nesneye eşit olup olmadığını belirler ValueTask<TResult> .Determines whether the specified ValueTask<TResult> object is equal to the current ValueTask<TResult> object. |
| GetAwaiter() |
Bu değer için bir awaiter oluşturur.Creates an awaiter for this value. |
| GetHashCode() |
Bu örneğe ilişkin karma kodu döndürür.Returns the hash code for this instance. |
| Preserve() |
ValueTask<TResult>Gelecekte herhangi bir noktada kullanılabilecek bir alır.Gets a ValueTask<TResult> that may be used at any point in the future. |
| ToString() |
Geçerli nesneyi temsil eden dizeyi döndürür.Returns a string that represents the current object. |
İşleçler
| Equality(ValueTask<TResult>, ValueTask<TResult>) |
, Eşitlik için iki değeri karşılaştırır.Compares two values for equality. |
| Inequality(ValueTask<TResult>, ValueTask<TResult>) |
İki değerin eşit olup olmadığını belirler ValueTask<TResult> .Determines whether two ValueTask<TResult> values are unequal. |