//****************************************************************************************************** // WaitHandleExtensions.cs - Gbtc // // Copyright © 2016, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this // file except in compliance with the License. You may obtain a copy of the License at: // // http://opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 12/05/2016 - J. Ritchie Carroll // Generated original version of source code. // //****************************************************************************************************** using System; using System.Collections.Generic; using System.Threading; using GSF.Collections; namespace GSF.Threading { /// /// Defines extension functions related to manipulation wait handle objects. /// public static class WaitHandleExtensions { /// /// Waits for all the specified elements to receive a signal. /// /// Collection of elements to operate on. /// A to observe, if any. /// true when every element has received a signal; otherwise the method never returns unless cancelled. /// is null. /// /// Using will cause all elements /// to be upgraded to a standard , these overloads allow similar functionality without /// incurring unconditional inflation of the underlying . /// public static bool WaitAll(this IEnumerable 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)); } /// /// Waits for all the specified elements to receive a signal. /// /// Collection of elements to operate on. /// A to observe. /// true when every element has received a signal; otherwise the method never returns unless cancelled. /// is null. /// /// Using will cause all elements /// to be upgraded to a standard , these overloads allow similar functionality without /// incurring unconditional inflation of the underlying . /// public static bool WaitAll(this IEnumerable resetEvents, System.Threading.CancellationToken cancellationToken) { if ((object)resetEvents == null) throw new ArgumentNullException(nameof(resetEvents)); return resetEvents.AllParallel(resetEvent => { resetEvent.Wait(cancellationToken); return !cancellationToken.IsCancellationRequested; }); } /// /// Waits for all the specified elements to receive a signal, using an integer value to specify the maximum time interval,in milliseconds, to wait. /// /// Collection of elements to operate on. /// The number of milliseconds to wait, or (-1) to wait indefinitely. /// A to observe, if any. /// true when every element has received a signal; otherwise false. /// is null. /// /// Using will cause all elements /// to be upgraded to a standard , these overloads allow similar functionality without /// incurring unconditional inflation of the underlying . /// public static bool WaitAll(this IEnumerable resetEvents, int timeout, CancellationToken cancellationToken = null) { if ((object)resetEvents == null) throw new ArgumentNullException(nameof(resetEvents)); if ((object)cancellationToken == null) return resetEvents.AllParallel(resetEvent => resetEvent.Wait(timeout)); return resetEvents.WaitAll(timeout, new CompatibleCancellationToken(cancellationToken)); } /// /// Waits for all the specified elements to receive a signal, using an integer value to specify the maximum time interval,in milliseconds, to wait. /// /// Collection of elements to operate on. /// The number of milliseconds to wait, or (-1) to wait indefinitely. /// A to observe. /// true when every element has received a signal; otherwise false. /// is null. /// /// Using will cause all elements /// to be upgraded to a standard , these overloads allow similar functionality without /// incurring unconditional inflation of the underlying . /// public static bool WaitAll(this IEnumerable resetEvents, int timeout, System.Threading.CancellationToken cancellationToken) { if ((object)resetEvents == null) throw new ArgumentNullException(nameof(resetEvents)); return resetEvents.AllParallel(resetEvent => resetEvent.Wait(timeout, cancellationToken)); } /// /// Waits for all the specified elements to receive a signal, using a value to specify the maximum time interval to wait. /// /// Collection of elements to operate on. /// A that represents the number of milliseconds to wait, or a that represents -1 milliseconds, to wait indefinitely. /// A to observe, if any. /// true when every element has received a signal; otherwise false. /// is null. /// /// Using will cause all elements /// to be upgraded to a standard , these overloads allow similar functionality without /// incurring unconditional inflation of the underlying . /// public static bool WaitAll(this IEnumerable resetEvents, TimeSpan timeout, CancellationToken cancellationToken = null) { if ((object)resetEvents == null) throw new ArgumentNullException(nameof(resetEvents)); if ((object)cancellationToken == null) return resetEvents.AllParallel(resetEvent => resetEvent.Wait(timeout)); return resetEvents.WaitAll(timeout, new CompatibleCancellationToken(cancellationToken)); } /// /// Waits for all the specified elements to receive a signal, using a value to specify the maximum time interval to wait. /// /// Collection of elements to operate on. /// A that represents the number of milliseconds to wait, or a that represents -1 milliseconds, to wait indefinitely. /// A to observe. /// true when every element has received a signal; otherwise false. /// is null. /// /// Using will cause all elements /// to be upgraded to a standard , these overloads allow similar functionality without /// incurring unconditional inflation of the underlying . /// public static bool WaitAll(this IEnumerable resetEvents, TimeSpan timeout, System.Threading.CancellationToken cancellationToken) { if ((object)resetEvents == null) throw new ArgumentNullException(nameof(resetEvents)); return resetEvents.AllParallel(resetEvent => resetEvent.Wait(timeout, cancellationToken)); } } }