/// <summary> /// Creates a new GRaff.IAsyncOperation`1 that will resolve when the first of the GRaff.IAsyncOperation objects resolves. /// The resolution value is the index of the operation that resolved in the array. /// </summary> /// <param name="operations">An array og GRaff.IAsyncOperation objects.</param> /// <returns>A GRaff.IAsyncOperation that will resolve when the first of the specified GRaff.IAsyncOperations is accepted.</returns> /// <remarks> /// If all operations are rejected, the aggregated operation will be rejected by an AggregateException, containing an array o /// System.Exception objects, where each exception corresponds respectively to each input operation. /// </remarks> public static IAsyncOperation <int> Any(params IAsyncOperation[] operations) { if (operations == null || operations.Length == 0) { return(Async.Capture(0)); } var deferred = new Deferred <int>(); var triggeredOperation = 0; var remainingOperations = operations.Length; var exceptions = new Exception[operations.Length]; for (int i = 0; i < operations.Length; i++) { int index = i; operations[i].ThenWait(() => { if (Interlocked.Exchange(ref triggeredOperation, 1) == 0) { deferred.Accept(index); } }); operations[i].Otherwise().ThenWait(ex => { exceptions[index] = ex; if (Interlocked.Decrement(ref remainingOperations) == 0) { deferred.Reject(new AggregateException(exceptions)); } }); } return(deferred.Operation); }