Table of Contents

Class ResultRacer<T>

Namespace
NanoByte.Common.Threading
Assembly
NanoByte.Common.dll

Helper for racing multiple operations against each other, providing the result of the first one that finishes.

public class ResultRacer<T> where T : notnull

Type Parameters

T

The type of the result.

Inheritance
object
ResultRacer<T>
Extension Methods

Constructors

ResultRacer(CancellationToken)

Helper for racing multiple operations against each other, providing the result of the first one that finishes.

public ResultRacer(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Used to cancel all pending operations.

Methods

GetResult()

Blocks until at least one call to TrySetResult(Func<CancellationToken, T?>) or TrySetResultAsync(Func<CancellationToken, Task<T?>>) succeeded and returns its result.

public T GetResult()

Returns

T

GetResultAsync()

Waits until at least one call to TrySetResult(Func<CancellationToken, T?>) or TrySetResultAsync(Func<CancellationToken, Task<T?>>) succeeded and returns its result.

public Task<T> GetResultAsync()

Returns

Task<T>

TrySetResult(Func<CancellationToken, T?>)

Trys to set a result, racing against other calls of this method.

public void TrySetResult(Func<CancellationToken, T?> factory)

Parameters

factory Func<CancellationToken, T>

A function that takes a cancellation token (triggered when another call won the race) and returns a possible result. Return null to indicate that the function was unable to provide a result. Exceptions (except System.OperationCanceledException) are passed through to GetResult() and GetResultAsync().

TrySetResultAsync(Func<CancellationToken, Task<T?>>)

Trys to set a result, racing against other calls of this method.

public Task TrySetResultAsync(Func<CancellationToken, Task<T?>> factory)

Parameters

factory Func<CancellationToken, Task<T>>

A function that takes a cancellation token (triggered when another call won the race) and returns a System.Threading.Tasks.Task<TResult> returning a possible result. Return null to indicate that the function was unable to provide a result. Exceptions (except System.OperationCanceledException) are passed through to GetResult() and GetResultAsync().

Returns

Task