Exemplo n.º 1
0
        void StopTimer()
        {
            var tx = clearTimer;

            clearTimer = null;
            if (tx != null)
            {
                tx.Dispose();
            }
        }
Exemplo n.º 2
0
        void StartTimer()
        {
            // 缓存数大于0才启动定时器
            if (ClearExpriod <= 0 || Items.Count < 1)
            {
                return;
            }

            if (clearTimer == null)
            {
                clearTimer = new TimerX(RemoveNotAlive, null, ClearExpriod * 1000, ClearExpriod * 1000);
            }
        }
Exemplo n.º 3
0
            public static void Remove(TimerX timer)
            {
                if (timer == null)
                {
                    return;
                }

                lock (timers)
                {
                    WriteLog("TimerX.Remove {0}", timer);

                    if (timers.Contains(timer))
                    {
                        timers.Remove(timer);
                    }
                }
            }
Exemplo n.º 4
0
            /// <summary>把定时器加入队列</summary>
            /// <param name="timer"></param>
            public static void Add(TimerX timer)
            {
                lock (timers)
                {
                    timers.Add(timer);

                    WriteLog("TimerX.Add {0}ms {1}", timer.Period, timer);

                    if (thread == null)
                    {
                        thread = new Thread(Process);
                        //thread.Name = "TimerX";
                        thread.Name         = "T";
                        thread.IsBackground = true;
                        thread.Start();
                    }

                    if (waitForTimer != null && waitForTimer.SafeWaitHandle != null && !waitForTimer.SafeWaitHandle.IsClosed)
                    {
                        waitForTimer.Set();
                    }
                }
            }
Exemplo n.º 5
0
            /// <summary>处理每一个定时器</summary>
            /// <param name="timer"></param>
            static void ProcessItem(TimerX timer)
            {
                // 删除过期的,为了避免占用过多CPU资源,TimerX禁止小于10ms的任务调度
                if (!timer.Callback.IsAlive || timer.Period < 10 && timer.Period > 0)
                {
                    // 周期0表示只执行一次
                    //if (timer.Period < 10 && timer.Period > 0) XTrace.WriteLine("为了避免占用过多CPU资源,TimerX禁止小于10ms的任务调度,关闭任务{0}", timer);
                    lock (timers)
                    {
                        timers.Remove(timer);
                        timer.Dispose();
                    }
                    return;
                }

                var ts = timer.NextTime - DateTime.Now;
                var d  = (Int32)ts.TotalMilliseconds;

                if (d > 0)
                {
                    // 缩小间隔,便于快速调用
                    if (d < period)
                    {
                        period = d;
                    }

                    return;
                }

                //WriteLog("TimerX.Process {0}", timer);

                try
                {
                    timer.Calling = true;

                    Action <Object> callback = timer.Callback;
                    // 线程池调用
                    if (timer.UseThreadPool)
                    {
                        //ThreadPoolX.QueueUserWorkItem(delegate() { callback(timer.State); });
                    }
                    else
                    {
                        callback(timer.State);
                    }
                }
                catch (ThreadAbortException) { throw; }
                catch (ThreadInterruptedException) { throw; }
                catch { }
                finally
                {
                    timer.Timers++;
                    timer.Calling  = false;
                    timer.NextTime = DateTime.Now.AddMilliseconds(timer.Period);

                    // 清理一次性定时器
                    if (timer.Period <= 0)
                    {
                        lock (timers)
                        {
                            timers.Remove(timer);
                            timer.Dispose();
                        }
                    }
                    if (timer.Period < period)
                    {
                        period = timer.Period;
                    }
                }
            }
Exemplo n.º 6
0
        /// <summary>延迟执行一个委托</summary>
        /// <param name="callback"></param>
        /// <param name="ms"></param>
        /// <returns></returns>
        public static TimerX Delay(WaitCallback callback, Int32 ms)
        {
            var timer = new TimerX(callback, null, ms, 0);

            return(timer);
        }