Пример #1
0
        /// <summary>
        /// 服务开始
        /// </summary>
        public void Start(TimerStrategy strategy, params object[] parameters)
        {
            try
            {
                TimeCalculator.CheckStrategy(strategy);

                this._Params = parameters;

                this.ScheduleStrategy = strategy;

                if (this.ScheduleStrategy.Delay == null)
                {
                    this.ScheduleStrategy.Delay = new TimeSpan(0);
                }

                this._SysTimer = new Timer(new TimerCallback(this.TimerIntervalRunning), AppDomain.CurrentDomain, this.ScheduleStrategy.Delay, this.ScheduleStrategy.Interval);
            }
            catch (Exception ex)
            {
                this.OnThrowException(ex);
            }
        }
Пример #2
0
        /// <summary>
        /// 间隔执行
        /// </summary>
        public void TimerIntervalRunning(object sender)
        {
            //检测是否到了执行服务程序的时间
            bool timeIsUp = true;

            if (this.ScheduleStrategy.TimerMode != TimerMode.Interval)
            {
                timeIsUp = TimeCalculator.TimeIsUp(this.ScheduleStrategy);
            }

            try
            {
                //时间到
                if (timeIsUp)
                {
                    //服务运行中
                    TaskStatus = TimerTaskStatus.Running;

                    //设置计时器,在无穷时间后再启用(实际上已经停止计时器计时了)
                    this._SysTimer.Change(Timeout.Infinite, Timeout.Infinite);

                    //开始处理
                    if (this.ScheduleStrategy.ReEntrant)
                    {
                        new Action(() =>
                        {
                            try
                            {
                                this.OnStart(this._Params);
                            }
                            catch (Exception ex)
                            {
                                this.OnThrowException(ex);//处理异常
                            }
                        }).BeginInvoke(null, null);
                    }
                    else
                    {
                        this.OnStart(this._Params);
                    }
                }
            }
            catch (Exception ex)
            {
                this.OnThrowException(ex);//处理异常
            }
            finally
            {
                //计时器存在,则重置休眠时间
                if (null != this._SysTimer)
                {
                    if (this.ScheduleStrategy.TimerMode == TimerMode.Interval)
                    {
                        //重新启用计时器
                        this._SysTimer.Change(this.ScheduleStrategy.Interval, this.ScheduleStrategy.Interval);
                    }
                    else
                    {
                        var interval = TimeCalculator.GetNextTimeUp(this.ScheduleStrategy);

                        if (interval.Ticks <= 0)
                        {
                            interval = TimeCalculator.GetNextTimeUp(this.ScheduleStrategy);
                        }

                        //重置
                        this._SysTimer.Change(interval, interval);
                    }
                }

                TaskStatus = TimerTaskStatus.Completed;
            }
        }