• 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
  • 相关阅读:
    技术的那些事儿_2_产品与工艺
    for与foreach再探讨
    技术的那些事儿_3_西方技术管理的精髓
    搭建免费的.Net开发环境
    CDN
    servu 系统服务看门狗,自动脱机补丁,自动启动
    .NET Remoting程序开发入门篇
    网管必知远程终端3389端口合理修改秘藉
    反射方法调用时的一个错误:参数计数不匹配( parameter count mismatch )
    VPS性能测试第一步:CPU测试
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2701965.html
Copyright © 2020-2023  润新知