/// <summary> /// Waits for all the specified <see cref="ManualResetEventSlim"/> elements to receive a signal. /// </summary> /// <param name="resetEvents">Collection of <see cref="ManualResetEventSlim"/> elements to operate on.</param> /// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken"/> to observe.</param> /// <returns><c>true</c> when every <see cref="ManualResetEventSlim"/> element has received a signal; otherwise the method never returns unless cancelled.</returns> /// <exception cref="ArgumentNullException"><paramref name="resetEvents"/> is <c>null</c>.</exception> /// <remarks> /// Using <see cref="WaitHandle.WaitAll(WaitHandle[])"/> will cause all <see cref="ManualResetEventSlim"/> elements /// to be upgraded to a standard <see cref="ManualResetEvent"/>, these overloads allow similar functionality without /// incurring unconditional inflation of the underlying <see cref="ManualResetEvent"/>. /// </remarks> public static bool WaitAll(this IEnumerable<ManualResetEventSlim> resetEvents, System.Threading.CancellationToken cancellationToken) { if ((object)resetEvents == null) throw new ArgumentNullException(nameof(resetEvents)); return resetEvents.AllParallel(resetEvent => { resetEvent.Wait(cancellationToken); return !cancellationToken.IsCancellationRequested; }); }
/// <summary> /// Waits for all the specified <see cref="ManualResetEventSlim"/> elements to receive a signal. /// </summary> /// <param name="resetEvents">Collection of <see cref="ManualResetEventSlim"/> elements to operate on.</param> /// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe, if any.</param> /// <returns><c>true</c> when every <see cref="ManualResetEventSlim"/> element has received a signal; otherwise the method never returns unless cancelled.</returns> /// <exception cref="ArgumentNullException"><paramref name="resetEvents"/> is <c>null</c>.</exception> /// <remarks> /// Using <see cref="WaitHandle.WaitAll(WaitHandle[])"/> will cause all <see cref="ManualResetEventSlim"/> elements /// to be upgraded to a standard <see cref="ManualResetEvent"/>, these overloads allow similar functionality without /// incurring unconditional inflation of the underlying <see cref="ManualResetEvent"/>. /// </remarks> public static bool WaitAll(this IEnumerable<ManualResetEventSlim> resetEvents, CancellationToken cancellationToken = null) { if ((object)resetEvents == null) throw new ArgumentNullException(nameof(resetEvents)); if ((object)cancellationToken == null) return resetEvents.AllParallel(resetEvent => { resetEvent.Wait(); return true; } ); return resetEvents.WaitAll(new CompatibleCancellationToken(cancellationToken)); }
/// <summary> /// Waits for all the specified <see cref="ManualResetEventSlim"/> elements to receive a signal, using a <see cref="TimeSpan"/> value to specify the maximum time interval to wait. /// </summary> /// <param name="resetEvents">Collection of <see cref="ManualResetEventSlim"/> elements to operate on.</param> /// <param name="timeout">A <see cref="TimeSpan"/> that represents the number of milliseconds to wait, or a <see cref="TimeSpan"/> that represents -1 milliseconds, to wait indefinitely.</param> /// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken"/> to observe.</param> /// <returns><c>true</c> when every <see cref="ManualResetEventSlim"/> element has received a signal; otherwise <c>false</c>.</returns> /// <exception cref="ArgumentNullException"><paramref name="resetEvents"/> is <c>null</c>.</exception> /// <remarks> /// Using <see cref="WaitHandle.WaitAll(WaitHandle[])"/> will cause all <see cref="ManualResetEventSlim"/> elements /// to be upgraded to a standard <see cref="ManualResetEvent"/>, these overloads allow similar functionality without /// incurring unconditional inflation of the underlying <see cref="ManualResetEvent"/>. /// </remarks> public static bool WaitAll(this IEnumerable<ManualResetEventSlim> resetEvents, TimeSpan timeout, System.Threading.CancellationToken cancellationToken) { if ((object)resetEvents == null) throw new ArgumentNullException(nameof(resetEvents)); return resetEvents.AllParallel(resetEvent => resetEvent.Wait(timeout, cancellationToken)); }