Table of Contents

Class TaskExtensions

Namespace
Plugin.BaseTypeExtensions
Assembly
Plugin.BaseTypeExtensions.dll

Provides extension methods for working with Task and Task<TResult> objects, including fire-and-forget and timeout patterns.

public static class TaskExtensions
Inheritance
TaskExtensions
Inherited Members

Methods

WithOptionalTimeoutInMs(Task, long, CancellationToken)

Sets up a timeout-monitor on the given task. This is essentially a wrapper around WaitAsync(TimeSpan) with one major difference: if the timeout is zero or negative then it gets interpreted as "wait forever" and the method will just return the task itself.

public static Task WithOptionalTimeoutInMs(this Task task, long timeout, CancellationToken cancellationToken = default)

Parameters

task Task

The task to monitor.

timeout long

The 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.

Remarks

  • If you want to monitor a TaskCompletionSource then it's strongly advised that you use WaitAndFossilizeTaskOnTimeoutAsync(TaskCompletionSource, long, CancellationToken) instead.

  • It's also strongly advised to always link the task you provide with a CancellationToken and pass the token to this method so as to minimize the risk of ending up with zombie tasks.

  • If you don't link your task with a cancellation token then bear in mind that in the case of a TimeoutException it's imperative that you perform the cleanup (cancellation) of your task yourself!

    Forgetting to do so can (under certain conditions) result in a memory leak and even worst a zombie-promise-task in runtime that can potentially chronically bog-down the performance of your application!

Exceptions

TimeoutException

Thrown when the task didn't complete within the specified timeout.

WithOptionalTimeoutInMs<T>(Task<T>, long, CancellationToken)

Sets up a timeout-monitor on the given task. This is essentially a wrapper around WaitAsync(TimeSpan) with one major difference: if the timeout is zero or negative then it gets interpreted as "wait forever" and the method will just return the task itself.

public static Task<T> WithOptionalTimeoutInMs<T>(this Task<T> task, long timeout, CancellationToken cancellationToken = default)

Parameters

task Task<T>

The task to monitor.

timeout long

The 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.

Type Parameters

T

Remarks

  • If you want to monitor a TaskCompletionSource then it's strongly advised that you use WaitAndFossilizeTaskOnTimeoutAsync(TaskCompletionSource, long, CancellationToken) instead.

  • It's also strongly advised to always link the task you provide with a CancellationToken and pass the token to this method so as to minimize the risk of ending up with zombie tasks.

  • If you don't link your task with a cancellation token then bear in mind that in the case of a TimeoutException it's imperative that you perform the cleanup (cancellation) of your task yourself!

    Forgetting to do so can (under certain conditions) result in a memory leak and even worst a zombie-promise-task in runtime that can potentially chronically bog-down the performance of your application!

Exceptions

TimeoutException

Thrown when the task didn't complete within the specified timeout.

WithTimeoutInMs(Task, long, CancellationToken)

Sets up a timeout-monitor on the given task. This is essentially a wrapper around WaitAsync(TimeSpan) with one major difference: it doesn't accept -1 as a means of "wait indefinitely" (we never considered this "shortcut value" to be a good idea so we just throw an argument-exception for all negative values).

public static Task WithTimeoutInMs(this Task task, long timeout, CancellationToken cancellationToken = default)

Parameters

task Task

The task to monitor.

timeout long

The timeout in milliseconds. Must be zero or positive. Negative values will cause a ArgumentException to be thrown.

cancellationToken CancellationToken

(optional) The cancellation token that co-monitors the waiting mechanism.

Returns

Task

The hybridized task that you can await on

Remarks

  • If you want to monitor a TaskCompletionSource then it's strongly advised that you use WaitAndFossilizeTaskOnTimeoutAsync(TaskCompletionSource, long, CancellationToken) instead.

  • It's also strongly advised to always link the task you provide with a CancellationToken and pass the token to this method so as to minimize the risk of ending up with zombie tasks.

  • If you don't link your task with a cancellation token then bear in mind that in the case of a TimeoutException it's imperative that you perform the cleanup (cancellation) of your task yourself!

    Forgetting to do so can (under certain conditions) result in a memory leak and even worst a zombie-promise-task in runtime that can potentially chronically bog-down the performance of your application!

Exceptions

ArgumentException

Thrown when the timeout is negative.

TimeoutException

Thrown when the task didn't complete within the specified timeout.

WithTimeoutInMs<T>(Task<T>, long, CancellationToken)

Sets up a timeout-monitor on the given task. This is essentially a wrapper around WaitAsync(TimeSpan) with one major difference: it doesn't accept -1 as a means of "wait indefinitely" (we never considered this "shortcut value" to be a good idea so we just throw an argument-exception for all negative values).

public static Task<T> WithTimeoutInMs<T>(this Task<T> task, long timeout, CancellationToken cancellationToken = default)

Parameters

task Task<T>

The task to monitor.

timeout long

The timeout in milliseconds. Must be zero or positive. Negative values will cause a ArgumentException to be thrown.

cancellationToken CancellationToken

(optional) The cancellation token that co-monitors the waiting mechanism.

Returns

Task<T>

The hybridized task that you can await on

Type Parameters

T

The type of the task

Remarks

  • If you want to monitor a TaskCompletionSource then it's strongly advised that you use WaitAndFossilizeTaskOnTimeoutAsync(TaskCompletionSource, long, CancellationToken) instead.

  • It's also strongly advised to always link the task you provide with a CancellationToken and pass the token to this method so as to minimize the risk of ending up with zombie tasks.

  • If you don't link your task with a cancellation token then bear in mind that in the case of a TimeoutException it's imperative that you perform the cleanup (cancellation) of your task yourself!

    Forgetting to do so can (under certain conditions) result in a memory leak and even worst a zombie-promise-task in runtime that can potentially chronically bog-down the performance of your application!

Exceptions

ArgumentException

Thrown when the timeout is negative.

TimeoutException

Thrown when the task didn't complete within the specified timeout.