1.引言

实际应用中,我们是定义一个作业,定时器然后定时去调用。前面章节都介绍的时间调度、事件过滤(EventFilter)任务方法(MethodCall) 这些都是这个作业零部件,这就是这节要介绍的Job。

2.Job

Scheduled Timer的Job是由时间调度器、任务方法,事件过滤器等组成,共同来达到预期的目的,单个不行,好像一个团队,主要代码如下

    /// <summary>
    /// 定时器作业
    /// </summary>
    public class TimerJob
    {
        public IScheduledItem Schedule;
        //是否同步
        public bool SyncronizedEvent = true;
        public IResultFilter Filter;
        public IMethodCall Method;
        //public IJobLog Log;
        public bool Enabled = true;

        private delegate void ExecuteHandler(object sender, DateTime EventTime, ExceptionEventHandler Error);
        private ExecuteHandler _ExecuteHandler;

        public TimerJob(IScheduledItem schedule, IMethodCall method)
        {
            Schedule = schedule;
            Method = method;
            _ExecuteHandler = new ExecuteHandler(ExecuteInternal);
        }

        public DateTime NextRunTime(DateTime time, bool includeStartTime)
        {
            if (!Enabled)
                return DateTime.MaxValue;
            return Schedule.NextRunTime(time, includeStartTime);
        }

        public void Execute(object sender, DateTime begin, DateTime end, ExceptionEventHandler error)
        {
            if (!Enabled)
                return;

            List<DateTime> eventList = new List<DateTime>();
            Schedule.AddEventsInInterval(begin, end, eventList);

            if (Filter != null)
                Filter.FilterResultsInInterval(begin, end, eventList);

            foreach (DateTime eventTime in eventList)
            {
                if (SyncronizedEvent)
                    _ExecuteHandler(sender, eventTime, error);
                else
                    _ExecuteHandler.BeginInvoke(sender, eventTime, error, null, null);
            }
        }

        private void ExecuteInternal(object sender, DateTime eventTime, ExceptionEventHandler error)
        {
            try
            {
                TimerParameterSetter setter = new TimerParameterSetter(eventTime, sender);
                Method.Execute(setter);
            }
            catch (Exception ex)
            {
                if (error != null)
                    try { error(this, new ExceptionEventArgs(eventTime, ex)); }
                    catch { }
            }
        }
    }

TimerJob的大致工作流程是,首先初始化时间调度器、任务方法、事件过滤器,再在执行作业时,先获取这个时间调度的时间集合,再进行事件过滤,再在有效的事件集合里循环执行任务方法。Scheduled Timer中一个定时器Timer不是只对应一个Job,而是一个Job集合中,Scheduled Timer的Job集合,代码很简单,对一个集合的操作,声明如下

    /// <summary>
    /// 计时器作业管理组
    /// </summary>
    public class TimerJobList
    {
        private List<TimerJob> _List;

        public TimerJobList()
        {
            _List = new List<TimerJob>();
        }

        public void Add(TimerJob Event)
        {
            _List.Add(Event);
        }

        public void Clear()
        {
            _List.Clear();
        }

        public DateTime NextRunTime(DateTime time)
        {
            DateTime next = DateTime.MaxValue;
            //从列表中,获得最低的日期时间。
            foreach (TimerJob Job in _List)
            {
                DateTime Proposed = Job.NextRunTime(time, true);
                next = (Proposed < next) ? Proposed : next;
            }
            return next;
        }

        public TimerJob[] Jobs
        {
            get { return _List.ToArray(); }
        }
    }

3.总结

作业比较简单,就是把之前的章节进行组合,主要看Job执行方法Execute,这也是定时器Timer的公共事件Elapsed事件将要调用的方法。

posted on 2012-09-25 09:13  Qlin  阅读(2171)  评论(9编辑  收藏  举报