Class ValueTaskCompletionSource<T>
Represents the producer side of ValueTask<TResult>.
Implements
Inherited Members
Namespace: DotNext.Threading.Tasks
Assembly: DotNext.Threading.dll
Syntax
public class ValueTaskCompletionSource<T> : ManualResetCompletionSource, IValueTaskSource<T>, IValueTaskSource, ISupplier<TimeSpan, CancellationToken, ValueTask>, ISupplier<TimeSpan, CancellationToken, ValueTask<T>>, IFunctional
Type Parameters
| Name | Description |
|---|---|
| T |
|
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
View SourceValueTaskCompletionSource(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
View SourceCleanUp()
Resets internal state of this source.
Declaration
protected override void CleanUp()
Overrides
View SourceCreateLinkedTaskCompletionSource(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. |
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 |
|
| InvalidOperationException | The source is in invalid state. |
GetCancellationResult(CancellationToken)
Called automatically when cancellation detected.
Declaration
protected virtual Result<T> GetCancellationResult(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.
GetTimeoutResult()
Called automatically when timeout detected.
Declaration
protected virtual Result<T> GetTimeoutResult()
Returns
| Type | Description |
|---|---|
| Result<T> | The result to be assigned to the task. |
Remarks
By default, this method assigns TimeoutException as the task result.
TrySetException<TOptions>(TOptions, Exception)
Attempts to complete the task unsuccessfully.
Declaration
public override sealed bool TrySetException<TOptions>(TOptions options, Exception e) where TOptions : ManualResetCompletionSource.ICompletionOptions, allows ref struct
Parameters
| Type | Name | Description |
|---|---|---|
| TOptions | options | The completion options. |
| 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. |
Type Parameters
| Name | Description |
|---|---|
| TOptions |
Overrides
View SourceTrySetResult(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. |
TrySetResult<TOptions>(TOptions, T)
Attempts to complete the task successfully.
Declaration
public bool TrySetResult<TOptions>(TOptions options, T value) where TOptions : ManualResetCompletionSource.ICompletionOptions, allows ref struct
Parameters
| Type | Name | Description |
|---|---|---|
| TOptions | options | The completion options. |
| 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. |
Type Parameters
| Name | Description |
|---|---|
| TOptions |