//[TestMethod]
        //[PlatformSpecific(TestPlatforms.Windows)] // names aren't supported on Unix
        public void WaitAll_SameNames()
        {
            Mutex[] wh = new Mutex[2];
            wh[0] = new Mutex(false, "test");
            wh[1] = new Mutex(false, "test");

            AssertExtensions.Throws <ArgumentException>(null, () => WaitHandle.WaitAll(wh));
        }
        public void WaitAny_SameHandles()
        {
            ManualResetEvent[] wh = new ManualResetEvent[2];
            wh[0] = new ManualResetEvent(true);
            wh[1] = wh[0];

            Assert.AreEqual(0, WaitHandle.WaitAny(wh));
        }
        public void WaitAll_SameHandles()
        {
            ManualResetEvent[] wh = new ManualResetEvent[2];
            wh[0] = new ManualResetEvent(true);
            wh[1] = wh[0];

            AssertExtensions.ThrowsAny <ArgumentException>(() => WaitHandle.WaitAll(wh));
        }
        public bool Wait(int millisecondsTimeout, CancellationToken cancellationToken)
        {
            if (millisecondsTimeout < -1)
            {
                throw new ArgumentOutOfRangeException("millisecondsTimeout");
            }

            ThrowIfDisposed();

            if (!IsSet)
            {
                SpinWait wait = new SpinWait();

                while (!IsSet)
                {
                    if (wait.Count < spinCount)
                    {
                        wait.SpinOnce();
                        continue;
                    }

                    break;
                }

                cancellationToken.ThrowIfCancellationRequested();

                if (IsSet)
                {
                    return(true);
                }

                WaitHandle handle = WaitHandle;

                if (cancellationToken.CanBeCanceled)
                {
                    var result = WaitHandle2.WaitAny(new[] { handle, cancellationToken.WaitHandle }, millisecondsTimeout);//, false);
                    if (result == 1)
                    {
                        throw new InternalOCE(cancellationToken);
                    }
                    if (result == WaitHandle2.WaitTimeout)
                    {
                        return(false);
                    }
                }
                else
                {
                    if (!handle.WaitOne(millisecondsTimeout, false))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
        public void WaitAll()
        {
            var handles = new ManualResetEvent[] {
                new ManualResetEvent(true),
                new ManualResetEvent(true),
                new ManualResetEvent(true)
            };

            Assert.IsTrue(WaitHandle.WaitAll(handles));
            Assert.IsTrue(WaitHandle.WaitAll(handles, 1));
            Assert.IsTrue(WaitHandle.WaitAll(handles, TimeSpan.FromMilliseconds(1)));

            handles[2].Reset();

            Assert.IsFalse(WaitHandle.WaitAll(handles, 1));
            Assert.IsFalse(WaitHandle.WaitAll(handles, TimeSpan.FromMilliseconds(1)));
        }
        public void WaitAny()
        {
            var handles = new ManualResetEvent[] {
                new ManualResetEvent(false),
                new ManualResetEvent(false),
                new ManualResetEvent(true)
            };

            Assert.AreEqual(2, WaitHandle.WaitAny(handles));
            Assert.AreEqual(2, WaitHandle.WaitAny(handles, 1));
            Assert.AreEqual(2, WaitHandle.WaitAny(handles, TimeSpan.FromMilliseconds(1)));

            handles[2].Reset();

            Assert.AreEqual(WaitHandle.WaitTimeout, WaitHandle.WaitAny(handles, 1));
            Assert.AreEqual(WaitHandle.WaitTimeout, WaitHandle.WaitAny(handles, TimeSpan.FromMilliseconds(1)));
        }
Пример #7
0
 /// <summary>
 /// When overridden in a derived class, blocks the current thread until the current instance receives a signal, using a TimeSpan to measure the time interval and specifying whether to exit the synchronization domain before the wait.
 /// </summary>
 /// <param name="timeout">A TimeSpan that represents the number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely.</param>
 /// <param name="notApplicableOnCE">Just pass false</param>
 /// <returns>true if the current instance receives a signal; otherwise, false.</returns>
 public bool WaitOne(TimeSpan timeout, bool notApplicableOnCE)
 {
     return(NativeMethods.WaitForSingleObject(this.Handle, WaitHandle2.ToTimeoutMilliseconds(timeout)) != NativeMethods.WAIT_TIMEOUT);
 }
Пример #8
0
 public bool Wait(TimeSpan timeout) => Wait(WaitHandle2.ToTimeoutMilliseconds(timeout));
 public void WaitTimeout()
 {
     Assert.AreEqual(WaitHandle.WaitTimeout, WaitHandle.WaitAny(new[] { new ManualResetEvent(false) }, 0));
 }
Пример #10
0
 public static bool Wait(object obj, TimeSpan timeout)
 => Wait(obj, WaitHandle2.ToTimeoutMilliseconds(timeout));
Пример #11
0
 public static void TryEnter(object obj, TimeSpan timeout, ref bool lockTaken)
 => TryEnter(obj, WaitHandle2.ToTimeoutMilliseconds(timeout), ref lockTaken);