/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queues"></param> /// <param name="ct"></param> /// <returns></returns> public static async Task <AsyncProducerConsumerQueue <T> .DequeueResult> TryDequeueFromAnyAsync <T>( this IEnumerable <AsyncProducerConsumerQueue <T> > queues, CancellationToken ct) { var abort = new TaskCompletionSource(); using (var cancellation = CancellationTokenHelper.FromTask(abort.Task)) { using (var aggregation = CancellationTokenHelper.Aggregate(cancellation.Token, ct)) { var token = aggregation.Token; var tasks = queues.Select(queue => queue.TryDequeueAsync(token, abort)); var results = await Task.WhenAll(tasks).ConfigureAwait(false); var result = results.FirstOrDefault(value => value.Success); if (null != result) { return(result); } ct.ThrowIfCancellationRequested(); return(AsyncProducerConsumerQueue <T> .FalseResult); } } }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queues"></param> /// <param name="item"></param> /// <param name="ct"></param> /// <returns></returns> public static async Task <AsyncProducerConsumerQueue <T> > TryEnqueueToAnyAsync <T>( this IEnumerable <AsyncProducerConsumerQueue <T> > queues, T item, CancellationToken ct) { var abort = new TaskCompletionSource(); using (var cancellation = CancellationTokenHelper.FromTask(abort.Task)) { using (var aggregation = CancellationTokenHelper.Aggregate(cancellation.Token, ct)) { var token = aggregation.Token; var tasks = queues.Select(queue => queue.TryEnqueueAsync(item, token, abort)); var results = await Task.WhenAll(tasks).ConfigureAwait(false); var candidate = results.FirstOrDefault(value => null != value); if (null == candidate) { ct.ThrowIfCancellationRequested(); } return(candidate); } } }