Show / Hide Table of Contents

Class ValueTaskCompletionSource<T>

Represents the producer side of ValueTask<TResult>.

Inheritance
object
ManualResetCompletionSource
ValueTaskCompletionSource<T>
Implements
IValueTaskSource<T>
IValueTaskSource
ISupplier<TimeSpan, CancellationToken, ValueTask>
IFunctional<Func<TimeSpan, CancellationToken, ValueTask>>
ISupplier<TimeSpan, CancellationToken, ValueTask<T>>
IFunctional<Func<TimeSpan, CancellationToken, ValueTask<T>>>
Inherited Members
ManualResetCompletionSource.InitialCompletionToken
ManualResetCompletionSource.Reset()
ManualResetCompletionSource.TryReset(out short)
ManualResetCompletionSource.AfterConsumed()
ManualResetCompletionSource.CompletionData
ManualResetCompletionSource.TrySetException(Exception)
ManualResetCompletionSource.TrySetCanceled(CancellationToken)
ManualResetCompletionSource.Status
ManualResetCompletionSource.IsCompleted
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: DotNext.Threading.Tasks
Assembly: DotNext.Threading.dll
Syntax
public class ValueTaskCompletionSource<T> : ManualResetCompletionSource, IValueTaskSource<T>, IValueTaskSource, ISupplier<TimeSpan, CancellationToken, ValueTask>, IFunctional<Func<TimeSpan, CancellationToken, ValueTask>>, ISupplier<TimeSpan, CancellationToken, ValueTask<T>>, IFunctional<Func<TimeSpan, CancellationToken, ValueTask<T>>>
Type Parameters
Name Description
T

The type the task result.

Remarks

In contrast to TaskCompletionSource<TResult>, this source is resettable. From performance point of view, the type offers minimal or zero memory allocation for the task itself (excluding continuations). See CreateTask(TimeSpan, CancellationToken) for more information. The instance of this type typically used in combination with object pool pattern because the instance can be reused for multiple tasks. AfterConsumed() method allows to capture the point in time when the source can be reused, e.g. returned to the pool.

Constructors

| Edit this page View Source

ValueTaskCompletionSource(bool)

Initializes a new completion source.

Declaration
public ValueTaskCompletionSource(bool runContinuationsAsynchronously = true)
Parameters
Type Name Description
bool runContinuationsAsynchronously

Indicates that continuations must be executed asynchronously.

Methods

| Edit this page View Source

CleanUp()

Resets internal state of this source.

Declaration
protected override void CleanUp()
Overrides
ManualResetCompletionSource.CleanUp()
| Edit this page View Source

CreateLinkedTaskCompletionSource(object?, TimeSpan, CancellationToken)

Creates a linked TaskCompletionSource<TResult> that can be used cooperatively to complete the task.

Declaration
public TaskCompletionSource<T> CreateLinkedTaskCompletionSource(object? userData, TimeSpan timeout, CancellationToken token)
Parameters
Type Name Description
object userData

The custom data to be associated with the current version of the task.

TimeSpan timeout

The timeout associated with the task.

CancellationToken token

The cancellation token that can be used to cancel the task.

Returns
Type Description
TaskCompletionSource<T>

A linked TaskCompletionSource<TResult>.

Exceptions
Type Condition
InvalidOperationException

The source is in invalid state.

| Edit this page View Source

CreateTask(TimeSpan, CancellationToken)

Creates a fresh task linked with this source.

Declaration
public ValueTask<T> CreateTask(TimeSpan timeout, CancellationToken token)
Parameters
Type Name Description
TimeSpan timeout

The timeout associated with the task.

CancellationToken token

The cancellation token that can be used to cancel the task.

Returns
Type Description
ValueTask<T>

A fresh uncompleted task.

Remarks

This method must be called after Reset().

Exceptions
Type Condition
ArgumentOutOfRangeException

timeout is less than zero but not equals to InfiniteTimeSpan.

InvalidOperationException

The source is in invalid state.

| Edit this page View Source

OnCanceled(CancellationToken)

Called automatically when cancellation detected.

Declaration
protected virtual Result<T> OnCanceled(CancellationToken token)
Parameters
Type Name Description
CancellationToken token

The token representing cancellation reason.

Returns
Type Description
Result<T>

The result to be assigned to the task.

Remarks

By default, this method assigns OperationCanceledException as the task result.

| Edit this page View Source

OnTimeout()

Called automatically when timeout detected.

Declaration
protected virtual Result<T> OnTimeout()
Returns
Type Description
Result<T>

The result to be assigned to the task.

Remarks

By default, this method assigns TimeoutException as the task result.

| Edit this page View Source

TrySetCanceled(short, CancellationToken)

Attempts to complete the task unsuccessfully.

Declaration
public bool TrySetCanceled(short completionToken, CancellationToken token)
Parameters
Type Name Description
short completionToken

The completion token previously obtained from CreateTask(TimeSpan, CancellationToken) method.

CancellationToken token

The canceled token.

Returns
Type Description
bool

true if the result is completed successfully; false if the task has been canceled or timed out.

| Edit this page View Source

TrySetCanceled(object?, short, CancellationToken)

Attempts to complete the task unsuccessfully.

Declaration
public bool TrySetCanceled(object? completionData, short completionToken, CancellationToken token)
Parameters
Type Name Description
object completionData

The data to be saved in CompletionData property that can be accessed from within AfterConsumed() method.

short completionToken

The completion token previously obtained from CreateTask(TimeSpan, CancellationToken) method.

CancellationToken token

The canceled token.

Returns
Type Description
bool

true if the result is completed successfully; false if the task has been canceled or timed out.

| Edit this page View Source

TrySetCanceled(object?, CancellationToken)

Attempts to complete the task unsuccessfully.

Declaration
public override sealed bool TrySetCanceled(object? completionData, CancellationToken token)
Parameters
Type Name Description
object completionData

The data to be saved in CompletionData property that can be accessed from within AfterConsumed() method.

CancellationToken token

The canceled token.

Returns
Type Description
bool

true if the result is completed successfully; false if the task has been canceled or timed out.

Overrides
ManualResetCompletionSource.TrySetCanceled(object?, CancellationToken)
| Edit this page View Source

TrySetException(short, Exception)

Attempts to complete the task unsuccessfully.

Declaration
public bool TrySetException(short completionToken, Exception e)
Parameters
Type Name Description
short completionToken

The completion token previously obtained from CreateTask(TimeSpan, CancellationToken) method.

Exception e

The exception to be returned to the consumer.

Returns
Type Description
bool

true if the result is completed successfully; false if the task has been canceled or timed out.

| Edit this page View Source

TrySetException(object?, Exception)

Attempts to complete the task unsuccessfully.

Declaration
public override sealed bool TrySetException(object? completionData, Exception e)
Parameters
Type Name Description
object completionData

The data to be saved in CompletionData property that can be accessed from within AfterConsumed() method.

Exception e

The exception to be returned to the consumer.

Returns
Type Description
bool

true if the result is completed successfully; false if the task has been canceled or timed out.

Overrides
ManualResetCompletionSource.TrySetException(object?, Exception)
| Edit this page View Source

TrySetException(object?, short, Exception)

Attempts to complete the task unsuccessfully.

Declaration
public bool TrySetException(object? completionData, short completionToken, Exception e)
Parameters
Type Name Description
object completionData

The data to be saved in CompletionData property that can be accessed from within AfterConsumed() method.

short completionToken

The completion token previously obtained from CreateTask(TimeSpan, CancellationToken) method.

Exception e

The exception to be returned to the consumer.

Returns
Type Description
bool

true if the result is completed successfully; false if the task has been canceled or timed out.

| Edit this page View Source

TrySetResult(short, T)

Attempts to complete the task successfully.

Declaration
public bool TrySetResult(short completionToken, T value)
Parameters
Type Name Description
short completionToken

The completion token previously obtained from CreateTask(TimeSpan, CancellationToken) method.

T value

The value to be returned to the consumer.

Returns
Type Description
bool

true if the result is completed successfully; false if the task has been canceled or timed out.

| Edit this page View Source

TrySetResult(object?, short, T)

Attempts to complete the task successfully.

Declaration
public bool TrySetResult(object? completionData, short completionToken, T value)
Parameters
Type Name Description
object completionData

The data to be saved in CompletionData property that can be accessed from within AfterConsumed() method.

short completionToken

The completion token previously obtained from CreateTask(TimeSpan, CancellationToken) method.

T value

The value to be returned to the consumer.

Returns
Type Description
bool

true if the result is completed successfully; false if the task has been canceled or timed out.

| Edit this page View Source

TrySetResult(object?, T)

Attempts to complete the task successfully.

Declaration
public bool TrySetResult(object? completionData, T value)
Parameters
Type Name Description
object completionData

The data to be saved in CompletionData property that can be accessed from within AfterConsumed() method.

T value

The value to be returned to the consumer.

Returns
Type Description
bool

true if the result is completed successfully; false if the task has been canceled or timed out.

| Edit this page View Source

TrySetResult(T)

Attempts to complete the task successfully.

Declaration
public bool TrySetResult(T value)
Parameters
Type Name Description
T value

The value to be returned to the consumer.

Returns
Type Description
bool

true if the result is completed successfully; false if the task has been canceled or timed out.

Implements

IValueTaskSource<TResult>
IValueTaskSource
ISupplier<T1, T2, TResult>
IFunctional<TDelegate>
ISupplier<T1, T2, TResult>
IFunctional<TDelegate>

Extension Methods

BasicExtensions.As<T>(T)
BasicExtensions.GetUserData<T>(T)
BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, ReadOnlySpan<T>)
ExpressionBuilder.Const<T>(T)
AsyncLockAcquisition.AcquireLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireLockAsync<T>(T, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, bool, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, bool, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, TimeSpan, CancellationToken)
LockAcquisition.AcquireReadLock<T>(T)
LockAcquisition.AcquireReadLock<T>(T, TimeSpan)
LockAcquisition.AcquireUpgradeableReadLock<T>(T)
LockAcquisition.AcquireUpgradeableReadLock<T>(T, TimeSpan)
LockAcquisition.AcquireWriteLock<T>(T)
LockAcquisition.AcquireWriteLock<T>(T, TimeSpan)

See Also

ValueTaskCompletionSource
  • Edit this page
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾