Class ResultRacer
Helper for racing multiple operations against each other, providing the result of the first one that finishes.
Namespace: NanoByte.Common.Threading
Assembly: NanoByte.Common.dll
Syntax
public static class ResultRacer : Object
Methods
For<TInput, TResult>(IEnumerable<TInput>, Func<TInput, CancellationToken, TResult>, CancellationToken)
Races an operation for multiple input elements against each other.
Declaration
public static ResultRacer<TResult> For<TInput, TResult>(IEnumerable<TInput> input, Func<TInput, CancellationToken, TResult> factory, CancellationToken cancellationToken = null)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TInput> | input | The input elements to try to produce outputs for. |
Func<TInput, CancellationToken, TResult> | factory | A function that takes an input element and 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 OperationCanceledException) are passed through to GetResult() and GetResultAsync(). |
CancellationToken | cancellationToken | Used to cancel all pending operations. |
Returns
Type | Description |
---|---|
ResultRacer<TResult> |
Type Parameters
Name | Description |
---|---|
TInput | The type of the input elements. |
TResult | The type of the result. |
For<TInput, TResult>(IEnumerable<TInput>, Func<TInput, CancellationToken, Task<TResult>>, CancellationToken)
Races an operation for multiple input elements against each other.
Declaration
public static ResultRacer<TResult> For<TInput, TResult>(IEnumerable<TInput> input, Func<TInput, CancellationToken, Task<TResult>> factory, CancellationToken cancellationToken = null)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TInput> | input | The input elements to try to produce outputs for. |
Func<TInput, CancellationToken, Task<TResult>> | factory | A function that takes an input element and a cancellation token (triggered when another call won the race) and returns a Task<TResult> returning a possible result. Return null to indicate that the function was unable to provide a result. Exceptions (except OperationCanceledException) are passed through to GetResult() and GetResultAsync(). |
CancellationToken | cancellationToken | Used to cancel all pending operations. |
Returns
Type | Description |
---|---|
ResultRacer<TResult> |
Type Parameters
Name | Description |
---|---|
TInput | The type of the input elements. |
TResult | The type of the result. |