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 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 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)));
        }
 public void WaitTimeout()
 {
     Assert.AreEqual(WaitHandle.WaitTimeout, WaitHandle.WaitAny(new[] { new ManualResetEvent(false) }, 0));
 }