1.引言

Scheduled Timer将定时器Timer进行封装成ScheduledTimer,很多Timer暴露的成员,都以私有化,并将上一节中的定时器作业(TimerJob)进行了集成。

2.ScheduledTimer

代码很简单,直接上代码

    public class ScheduleTimer
    {
        public IEventStorage EventStorage = new LocalEventStorage();
        public event ExceptionEventHandler Error;
        private static TimeSpan MAX_INTERVAL = new TimeSpan(0, 1, 0);
        private DateTime _LastTime;
        private Timer _Timer;
        private TimerJobList _Jobs;
        private volatile bool _StopFlag;

        public ScheduleTimerBase()
        {
            _Timer = new Timer();
            _Timer.AutoReset = false;
            _Timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
            _Jobs = new TimerJobList();
            _LastTime = DateTime.MaxValue;
        }

        public void AddJob(IScheduledItem schedule, Delegate f, params object[] Params)
        {
            _Jobs.Add(new TimerJob(schedule, new DelegateMethodCall(f, Params)));
        }

        public void AddAsyncJob(IScheduledItem schedule, Delegate f, params object[] Params)
        {
            TimerJob Event = new TimerJob(schedule, new DelegateMethodCall(f, Params));
            Event.SyncronizedEvent = false;
            _Jobs.Add(Event);
        }     
        public void AddJob(TimerJob Event)
        {
            _Jobs.Add(Event);
        }

        public void ClearJobs()
        {
            _Jobs.Clear();
        }

        public void Start()
        {
            _StopFlag = false;
            QueueNextTime(EventStorage.ReadLastTime());
        }

        public void Stop()
        {
            _StopFlag = true;
            _Timer.Stop();
        }

        public void Dispose()
        {
            if (_Timer != null)
                _Timer.Dispose();
        }

        private double NextInterval(DateTime thisTime)
        {
            TimeSpan interval = _Jobs.NextRunTime(thisTime) - thisTime;
            if (interval > MAX_INTERVAL)
                interval = MAX_INTERVAL;

            return (interval.TotalMilliseconds == 0) ? 1 : interval.TotalMilliseconds;
        }

        private void QueueNextTime(DateTime thisTime)
        {
            _Timer.Interval = NextInterval(thisTime);
            System.Diagnostics.Debug.WriteLine(_Timer.Interval);
            _LastTime = thisTime;
            EventStorage.RecordLastTime(thisTime);
            _Timer.Start();
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                if (_Jobs == null)
                    return;

                _Timer.Stop();

                foreach (TimerJob Event in _Jobs.Jobs)
                {
                    try
                    {
                        Event.Execute(this, _LastTime, e.SignalTime, Error);
                    }
                    catch (Exception ex)
                    {
                        OnError(DateTime.Now, Event, ex);
                    }
                }
            }
            catch (Exception ex)
            {
                OnError(DateTime.Now, null, ex);
            }
            finally
            {
                if (_StopFlag == false)
                    QueueNextTime(e.SignalTime);
            }
        }

        private void OnError(DateTime eventTime, TimerJob job, Exception e)
        {
            if (Error == null)
                return;

            try
            {
                Error(this, new ExceptionEventArgs(eventTime, e));
            }
            catch (Exception) { }
        }
    }

暴露出的成员很简单,有事件存储、异常事件、启动、停止、添加作业、清空作业。

简单介绍一下流程:

  1. 声明一个ScheduleTimer
  2. 添加一个作业
  3. 启动定时器Start方法,EventStorage.ReadLastTime() 读取事件持久化时间,设置间隔时间
  4. 执行Timer的Elapsed事件,执行Job,记录 EventStorage

各个成员具体执行,请看之前的各个章节介绍。

3.总结

Scheduled Timer基本介绍了,还有一些 异常事件、回调事件没有介绍,都很简单,下面一个章节,将会进行总结一下。

posted on 2012-09-25 17:28  Qlin  阅读(1977)  评论(0编辑  收藏  举报