Class ValueTaskCompletionSource
Represents the producer side of ValueTask.
Implements
Inherited Members
Namespace: DotNext.Threading.Tasks
Assembly: DotNext.Threading.dll
Syntax
public class ValueTaskCompletionSource : ManualResetCompletionSource, IValueTaskSource, ISupplier<TimeSpan, CancellationToken, ValueTask>, IFunctional<Func<TimeSpan, CancellationToken, ValueTask>>
Remarks
See description of ValueTaskCompletionSource<T> for more information about behavior of the completion source.
Constructors
| Edit this page 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
| Edit this page View SourceCleanUp()
Resets internal state of this source.
Declaration
protected override void CleanUp()
Overrides
| Edit this page View SourceCreateLinkedTaskCompletionSource(object?, TimeSpan, CancellationToken)
Creates a linked TaskCompletionSource that can be used cooperatively to complete the task.
Declaration
public TaskCompletionSource 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 | A linked TaskCompletionSource. |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | The source is in invalid state. |
CreateTask(TimeSpan, CancellationToken)
Creates a fresh task linked with this source.
Declaration
public ValueTask 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 | A fresh uncompleted task. |
Remarks
This method must be called after Reset().
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException |
|
| InvalidOperationException | The source is in invalid state. |
OnCanceled(CancellationToken)
Called automatically when cancellation detected.
Declaration
protected virtual Exception? OnCanceled(CancellationToken token)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | token | The token representing cancellation reason. |
Returns
| Type | Description |
|---|---|
| Exception | The exception representing task result; or null to complete successfully. |
Remarks
By default, this method returns OperationCanceledException as the task result.
OnTimeout()
Called automatically when timeout detected.
Declaration
protected virtual Exception? OnTimeout()
Returns
| Type | Description |
|---|---|
| Exception | The exception representing task result; or null to complete successfully. |
Remarks
By default, this method returns TimeoutException as the task result.
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. |
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
| Edit this page View SourceTrySetException(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. |
TrySetResult()
Attempts to complete the task successfully.
Declaration
public bool TrySetResult()
Returns
| Type | Description |
|---|---|
| bool | true if the result is completed successfully; false if the task has been canceled or timed out. |
TrySetResult(object?)
Attempts to complete the task successfully.
Declaration
public bool TrySetResult(object? completionData)
Parameters
| Type | Name | Description |
|---|---|---|
| object | completionData | The data to be saved in CompletionData property that can be accessed from within AfterConsumed() method. |
Returns
| Type | Description |
|---|---|
| bool | true if the result is completed successfully; false if the task has been canceled or timed out. |
TrySetResult(object?, short)
Attempts to complete the task successfully.
Declaration
public bool TrySetResult(object? completionData, short completionToken)
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. |
Returns
| Type | Description |
|---|---|
| bool | true if the result is completed successfully; false if the task has been canceled or timed out. |
TrySetResult(object?, short?, Exception?, out bool)
Tries to set the result of this source without resuming the ValueTask consumer.
Declaration
protected bool TrySetResult(object? completionData, short? completionToken, Exception? e, out bool resumable)
Parameters
| Type | Name | Description |
|---|---|---|
| object | completionData | The completion data to be assigned to CompletionData property. |
| short? | completionToken | The optional completion token. |
| Exception | e | The exception to be stored as the result of ValueTask. |
| bool | resumable | true if NotifyConsumer() needs to be called to resume the consumer of ValueTask. |
Returns
| Type | Description |
|---|---|
| bool | true if this source is completed successfully; false if this source was completed previously. |