zoukankan      html  css  js  c++  java
  • ScheduleTimer

    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基本介绍了,还有一些 异常事件、回调事件没有介绍,都很简单,下面一个章节,将会进行总结一下。

    作者:Qlin 
    出处:http://www.cnblogs.com/qqlin/ 
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

    标签: timer
  • 相关阅读:
    mitmproxy抓包工具
    java基础|int和Integer的区别
    Vue|退出功能
    Vue|分页处理
    apt-get本地软件源搭建
    rqt_plot报错
    创建ROS 工作空间时出现:程序“catkin_init_workspace”尚未安装,程序“catkin_make”尚未安装。
    ubuntu16.04安装ROS
    debian及Ubuntu各版本下载地址获取
    解决sudo rosdep init和rosdep update的错误
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2701965.html
Copyright © 2011-2022 走看看