Class TaskCompletionSourceExtensions
- Namespace
- Plugin.BaseTypeExtensions
- Assembly
- Plugin.BaseTypeExtensions.dll
A family of extension methods that ensure that the TaskCompletionSource is properly cleaned up in the case of a timeout either
from the cancellation token or the timeout specified.
Always bear in mind that if the TaskCompletionSource is never completed, its task will never complete. Even though the underlying Task is not actually in a "scheduler" (since TCS tasks are Promise Tasks) never completing tasks, of any type, is generally considered a bug.
public static class TaskCompletionSourceExtensions
- Inheritance
-
TaskCompletionSourceExtensions
- Inherited Members
Methods
TrySetResultOrException(TaskCompletionSource, Exception?)
Attempts to complete the TaskCompletionSource with success or an exception.
public static bool TrySetResultOrException(this TaskCompletionSource tcs, Exception? exception = null)
Parameters
tcs
TaskCompletionSourceThe TaskCompletionSource to complete.
exception
ExceptionThe exception to set if provided. If null, the task will be marked as successfully completed.
Returns
- bool
true
if the operation was successful; otherwise,false
if the task was already completed.
TrySetResultOrException<T>(TaskCompletionSource<T>, T, Exception?)
Attempts to complete the TaskCompletionSource<TResult> with a result or an exception.
public static bool TrySetResultOrException<T>(this TaskCompletionSource<T> tcs, T result, Exception? exception = null)
Parameters
tcs
TaskCompletionSource<T>The TaskCompletionSource<TResult> to complete.
result
TThe result to set if no exception is provided.
exception
ExceptionThe exception to set if provided. If null, the result will be used instead.
Returns
- bool
true
if the operation was successful; otherwise,false
if the task was already completed.
Type Parameters
T
The type of the result.
WaitAndFossilizeTaskOnOptionalTimeoutAsync(TaskCompletionSource, long, CancellationToken)
Sets up a timeout-monitor on the given task. This is essentially a wrapper around WaitAsync(TimeSpan)
with two major differences:
- If the timeout is zero or negative then it gets interpreted as "wait forever" and the method will just return the task itself.
- Most importantly, in the case of a TimeoutException this method makes sure to properly cleanup (cancel) the task itself so that you won't have to (it's so easy to forget this and it's a common source of memory-leaks and zombie-promise-tasks that can cripple the system!).
public static Task WaitAndFossilizeTaskOnOptionalTimeoutAsync(this TaskCompletionSource tcs, long timeoutInMilliseconds, CancellationToken cancellationToken = default)
Parameters
tcs
TaskCompletionSourceThe task to monitor with a timeout.
timeoutInMilliseconds
longThe timeout in milliseconds. If zero or negative it gets interpreted as "wait forever" and the method will just return the task itself.
cancellationToken
CancellationToken(optional) The cancellation token that co-monitors the waiting mechanism.
Returns
- Task
The hybridized task that you can await on if you have provided a positive timeout. The task itself otherwise.
Exceptions
- TimeoutException
Thrown when the task didn't complete within the specified timeout.
WaitAndFossilizeTaskOnOptionalTimeoutAsync(TaskCompletionSource, TimeSpan, CancellationToken)
Sets up a timeout-monitor on the given task. This is essentially a wrapper around WaitAsync(TimeSpan)
with two major differences:
- If the timeout is zero or negative then it gets interpreted as "wait forever" and the method will just return the task itself.
- Most importantly, in the case of a TimeoutException this method makes sure to properly cleanup (cancel) the task itself so that you won't have to (it's so easy to forget this and it's a common source of memory-leaks and zombie-promise-tasks that can cripple the system!)
public static Task WaitAndFossilizeTaskOnOptionalTimeoutAsync(this TaskCompletionSource tcs, TimeSpan timespan, CancellationToken cancellationToken = default)
Parameters
tcs
TaskCompletionSourceThe task to monitor with a timeout.
timespan
TimeSpanThe amount of time to wait for before throwing a TimeoutException. If zero or negative it gets interpreted as "wait forever" and the method will just return the task itself.
cancellationToken
CancellationToken(optional) The cancellation token that co-monitors the waiting mechanism.
Returns
- Task
The hybridized task that you can await on if you have provided a positive timeout. The task itself otherwise.
Exceptions
- TimeoutException
Thrown when the task didn't complete within the specified timeout.
WaitAndFossilizeTaskOnOptionalTimeoutAsync<T>(TaskCompletionSource<T>, long, CancellationToken)
Sets up a timeout-monitor on the given task. This is essentially a wrapper around WaitAsync(TimeSpan)
with two major differences:
- If the timeout is zero or negative then it gets interpreted as "wait forever" and the method will just return the task itself.
- Most importantly, in the case of a TimeoutException this method makes sure to properly cleanup (cancel) the task itself so that you won't have to (it's so easy to forget this and it's a common source of memory-leaks and zombie-promise-tasks that can cripple the system!).
public static Task<T> WaitAndFossilizeTaskOnOptionalTimeoutAsync<T>(this TaskCompletionSource<T> tcs, long timeoutInMilliseconds, CancellationToken cancellationToken = default)
Parameters
tcs
TaskCompletionSource<T>The task to monitor with a timeout.
timeoutInMilliseconds
longThe timeout in milliseconds. If zero or negative it gets interpreted as "wait forever" and the method will just return the task itself.
cancellationToken
CancellationToken(optional) The cancellation token that co-monitors the waiting mechanism.
Returns
- Task<T>
The hybridized task that you can await on if you have provided a positive timeout. The task itself otherwise.
Type Parameters
T
Exceptions
- TimeoutException
Thrown when the task didn't complete within the specified timeout.
WaitAndFossilizeTaskOnOptionalTimeoutAsync<T>(TaskCompletionSource<T>, TimeSpan, CancellationToken)
Sets up a timeout-monitor on the given task. This is essentially a wrapper around WaitAsync(TimeSpan)
with two major differences:
- If the timeout is zero or negative then it gets interpreted as "wait forever" and the method will just return the task itself.
- Most importantly, in the case of a TimeoutException this method makes sure to properly cleanup (cancel) the task itself so that you won't have to (it's so easy to forget this and it's a common source of memory-leaks and zombie-promise-tasks that can cripple the system!).
public static Task<T> WaitAndFossilizeTaskOnOptionalTimeoutAsync<T>(this TaskCompletionSource<T> tcs, TimeSpan timespan, CancellationToken cancellationToken = default)
Parameters
tcs
TaskCompletionSource<T>The task to monitor with a timeout.
timespan
TimeSpanThe amount of time to wait for before throwing a TimeoutException. If zero or negative it gets interpreted as "wait forever" and the method will just return the task itself.
cancellationToken
CancellationToken(optional) The cancellation token that co-monitors the waiting mechanism.
Returns
- Task<T>
The hybridized task that you can await on if you have provided a positive timeout. The task itself otherwise.
Type Parameters
T
Exceptions
- TimeoutException
Thrown when the task didn't complete within the specified timeout.
WaitAndFossilizeTaskOnTimeoutAsync(TaskCompletionSource, long, CancellationToken)
Sets up a timeout-monitor on the given task. This is essentially a wrapper around WaitAsync(TimeSpan)
with two major differences:
- If the timeout is negative you will get an ArgumentException thrown (WaitAsync() allows -1 as a special-case for "wait forever"
but this method doesn't allow that special case)
- Most importantly, in the case of a TimeoutException this method makes sure to properly cleanup (cancel) the task itself so that you won't have to (it's so easy to forget this and it's a common source of memory-leaks and zombie-promise-tasks that can cripple the system!).
public static Task WaitAndFossilizeTaskOnTimeoutAsync(this TaskCompletionSource tcs, long timeoutInMilliseconds, CancellationToken cancellationToken = default)
Parameters
tcs
TaskCompletionSourceThe task to monitor with a timeout.
timeoutInMilliseconds
longThe timeout in milliseconds. If zero or negative it gets interpreted as "wait forever" and the method will just return the task itself.
cancellationToken
CancellationToken(optional) The cancellation token that co-monitors the waiting mechanism.
Returns
- Task
The hybridized task that you can await on.
Exceptions
- TimeoutException
Thrown when the task didn't complete within the specified timeout.
WaitAndFossilizeTaskOnTimeoutAsync(TaskCompletionSource, TimeSpan, CancellationToken)
Sets up a timeout-monitor on the given task. This is essentially a wrapper around WaitAsync(TimeSpan)
with two major differences:
- If the timeout is negative you will get an ArgumentException thrown (WaitAsync() allows -1 as a special-case for "wait forever"
but this method doesn't allow that special case)
- Most importantly, in the case of a TimeoutException this method makes sure to properly cleanup (cancel) the task itself so that you won't have to (it's so easy to forget this and it's a common source of memory-leaks and zombie-promise-tasks that can cripple the system!).
public static Task WaitAndFossilizeTaskOnTimeoutAsync(this TaskCompletionSource tcs, TimeSpan timespan, CancellationToken cancellationToken = default)
Parameters
tcs
TaskCompletionSourceThe task to monitor with a timeout.
timespan
TimeSpanThe amount of time to wait for before throwing a TimeoutException. If zero or negative it gets interpreted as "wait forever" and the method will just return the task itself.
cancellationToken
CancellationToken(optional) The cancellation token that co-monitors the waiting mechanism.
Returns
- Task
The hybridized task that you can await on.
Exceptions
- TimeoutException
Thrown when the task didn't complete within the specified timeout.
WaitAndFossilizeTaskOnTimeoutAsync<T>(TaskCompletionSource<T>, long, CancellationToken)
Sets up a timeout-monitor on the given task. This is essentially a wrapper around WaitAsync(TimeSpan)
with two major differences:
- If the timeout is negative you will get an ArgumentException thrown (WaitAsync() allows -1 as a special-case for "wait forever"
but this method doesn't allow that special case)
- Most importantly, in the case of a TimeoutException this method makes sure to properly cleanup (cancel) the task itself so that you won't have to (it's so easy to forget this and it's a common source of memory-leaks and zombie-promise-tasks that can cripple the system!).
public static Task<T> WaitAndFossilizeTaskOnTimeoutAsync<T>(this TaskCompletionSource<T> tcs, long timeoutInMilliseconds, CancellationToken cancellationToken = default)
Parameters
tcs
TaskCompletionSource<T>The task to monitor with a timeout.
timeoutInMilliseconds
longThe timeout in milliseconds. If zero or negative it gets interpreted as "wait forever" and the method will just return the task itself.
cancellationToken
CancellationToken(optional) The cancellation token that co-monitors the waiting mechanism.
Returns
- Task<T>
Type Parameters
T
Exceptions
- TimeoutException
Thrown when the task didn't complete within the specified timeout.
WaitAndFossilizeTaskOnTimeoutAsync<T>(TaskCompletionSource<T>, TimeSpan, CancellationToken)
Sets up a timeout-monitor on the given task. This is essentially a wrapper around WaitAsync(TimeSpan)
with two major differences:
- If the timeout is negative you will get an ArgumentException thrown (WaitAsync() allows -1 as a special-case for "wait forever"
but this method doesn't allow that special case)
- Most importantly, in the case of a TimeoutException this method makes sure to properly cleanup (cancel) the task itself so that you won't have to (it's so easy to forget this and it's a common source of memory-leaks and zombie-promise-tasks that can cripple the system!).
public static Task<T> WaitAndFossilizeTaskOnTimeoutAsync<T>(this TaskCompletionSource<T> tcs, TimeSpan timespan, CancellationToken cancellationToken = default)
Parameters
tcs
TaskCompletionSource<T>The task to monitor with a timeout.
timespan
TimeSpanThe amount of time to wait for before throwing a TimeoutException. If zero or negative it gets interpreted as "wait forever" and the method will just return the task itself.
cancellationToken
CancellationToken(optional) The cancellation token that co-monitors the waiting mechanism.
Returns
- Task<T>
Type Parameters
T
Exceptions
- TimeoutException
Thrown when the task didn't complete within the specified timeout.