示例#1
0
        /// <summary>
        /// Waits until the time is elapsed.
        /// </summary>
        /// <param name="untilDate">The until date.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        public static void WaitUntil(DateTime untilDate, CancellationToken cancellationToken = default)
        {
            void Callback(IWaitEvent waitEvent)
            {
                try
                {
                    waitEvent.Complete();
                    waitEvent.Begin();
                }
                catch
                {
                    // ignore
                }
            }

            using (var delayLock = WaitEventFactory.Create(true))
            {
                using (var _ = new ExclusiveTimer(() => Callback(delayLock), 0, 15))
                {
                    while (!cancellationToken.IsCancellationRequested && DateTime.UtcNow < untilDate)
                    {
                        delayLock.Wait();
                    }
                }
            }
        }
示例#2
0
        private void DelayThreadPool()
        {
            if (_delayEvent == null)
            {
                _delayEvent = WaitEventFactory.Create(isCompleted: true, useSlim: true);
            }

            _delayEvent.Begin();
            ThreadPool.QueueUserWorkItem(s =>
            {
                DelaySleep();
                _delayEvent.Complete();
            });

            _delayEvent.Wait();
        }