Class ResultRacer<T>
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 class ResultRacer<T> : Object
Type Parameters
Name | Description |
---|---|
T | The type of the result. |
Constructors
ResultRacer(CancellationToken)
Helper for racing multiple operations against each other, providing the result of the first one that finishes.
Declaration
public ResultRacer(CancellationToken cancellationToken = null)
Parameters
Type | Name | Description |
---|---|---|
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.
Declaration
public T GetResult()
Returns
Type | Description |
---|---|
T |
GetResultAsync()
Waits until at least one call to TrySetResult(Func<CancellationToken, T>) or TrySetResultAsync(Func<CancellationToken, Task<T>>) succeeded and returns its result.
Declaration
public Task<T> GetResultAsync()
Returns
Type | Description |
---|---|
Task<T> |
TrySetResult(Func<CancellationToken, T>)
Trys to set a result, racing against other calls of this method.
Declaration
public void TrySetResult(Func<CancellationToken, T> factory)
Parameters
Type | Name | Description |
---|---|---|
Func<CancellationToken, T> | factory | 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 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.
Declaration
public Task TrySetResultAsync(Func<CancellationToken, Task<T>> factory)
Parameters
Type | Name | Description |
---|---|---|
Func<CancellationToken, Task<T>> | factory | A function that takes 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(). |
Returns
Type | Description |
---|---|
Task |