Exemplo n.º 1
0
        /// <summary>
        /// 刷新时间
        /// </summary>
        /// <param name="state"></param>
        private static void refreshTime(object state)
        {
            System.Threading.Interlocked.Increment(ref refreshTimeThreadCount);
            DateTime now = DateTime.Now;

            Now    = now;
            UtcNow = now.localToUniversalTime();
            timer.Change(TimerInterval = 1000L - now.Millisecond, -1);

            do
            {
                long nextSecondTicks = NextSecondTicks;
                if (nextSecondTicks <= Now.Ticks)
                {
                    if (System.Threading.Interlocked.CompareExchange(ref NextSecondTicks, nextSecondTicks + TimeSpan.TicksPerSecond, nextSecondTicks) == nextSecondTicks)
                    {
                        System.Threading.Interlocked.Increment(ref CurrentSeconds);
                        try
                        {
                            SecondTimerNode node = SecondNodeLink.End;
                            if (node != null)
                            {
                                SecondTimerNode.LinkOnTimer(node);
                            }

                            if (System.Threading.Interlocked.CompareExchange(ref InternalTaskArray.TimerLinkLock.SleepFlag, 1, 0) == 0)
                            {
                                try
                                {
                                    ThreadPool.CheckExit();

                                    InternalTaskArray.OnTimer();
                                }
                                finally { System.Threading.Interlocked.Exchange(ref InternalTaskArray.TimerLinkLock.SleepFlag, 0); }
                            }

                            if (System.Threading.Interlocked.CompareExchange(ref TaskArray.TimerLinkLock.SleepFlag, 1, 0) == 0)
                            {
                                try
                                {
                                    TaskArray.OnTimer();
                                }
                                finally { System.Threading.Interlocked.Exchange(ref TaskArray.TimerLinkLock.SleepFlag, 0); }
                            }
                        }
                        catch (Exception exception)
                        {
                            AutoCSer.LogHelper.Exception(exception, "全局定时任务错误中断", LogLevel.AutoCSer | LogLevel.Exception | LogLevel.Fatal);
                        }
                    }
                }
                else
                {
                    System.Threading.Interlocked.Decrement(ref refreshTimeThreadCount);
                    return;
                }
            }while (true);
        }