예제 #1
0
        /// <summary>
        /// 策略变更
        /// </summary>
        /// <param name="config"></param>
        public static void CheckStrategy(TimerStrategy config)
        {
            var Config = config;

            if (Config == null || Config.StrategyByDay == null)
            {
                throw new Exception("定时器时间配置异常!");
            }
            if (Config == null)
            {
                throw new Exception("定时器时间配置异常!");
            }

            switch (Config.TimerMode)
            {
            case TimerMode.OnTimes:
                if (Config.OnTimes == null || Config.OnTimes.Count == 0)
                {
                    throw new Exception("定时器时间配置异常,OnTimes(取值不能为空)!");
                }
                Config.OnTimes = TimerStrategyManager.SortAndDistinct(Config.OnTimes);
                break;

            case TimerMode.EveryDay:
                return;
            }
        }
예제 #2
0
        /// <summary>
        /// 读取自定义配置节点
        /// </summary>
        /// <param name="parent">父结点</param>
        /// <param name="configContext">配置上下文</param>
        /// <param name="section">配置区</param>
        /// <returns></returns>
        object IConfigurationSectionHandler.Create(object parent, object configContext, XmlNode section)
        {
            StrategyConfig = new TimerStrategyConfig();

            var config = new List <TimerStrategy>();

            foreach (XmlNode node in section.ChildNodes)
            {
                if (node.NodeType == XmlNodeType.Element)
                {
                    switch (node.Name.ToLower())
                    {
                    case "default":
                        StrategyConfig.Default = node.InnerText;
                        break;

                    case "timerstrategy":
                        var temp = new TimerStrategy();
                        SetTimerConfigValue(temp, node);
                        config.Add(temp);
                        break;
                    }
                }
            }

            StrategyConfig.Config = config.ToArray();

            return(StrategyConfig);
        }
예제 #3
0
        /// <summary>
        /// 获取下一次指定配置的时间是多少
        /// </summary>
        /// <param name="strategy"></param>
        /// <returns></returns>
        public static DateTime GetNextDateTime(TimerStrategy strategy)
        {
            DateTime dt = DateTime.Now;

            TimeSpan MinTimeByDay;
            bool     hasExpectDate;

            TimeSpan time = GetNextTimeConfig(strategy.StrategyByDay, dt, out MinTimeByDay, out hasExpectDate);

            DateTime now, target;

            switch (strategy.TimerMode)
            {
            case TimerMode.OnTimes:
                dt = GetTargetDateTimeByOnTime(dt, strategy.OnTimes);
                break;

            case TimerMode.EveryDay:
                #region (Checked)每天指定某时执行一次
                now    = new DateTime(1, 1, 1, dt.Hour, dt.Minute, dt.Second);
                target = new DateTime(1, 1, 1, time.Hours, time.Minutes, time.Seconds);
                if (now.Ticks >= target.Ticks)
                {
                    dt = dt.AddDays(1.0);                                   //如果当前时间小于指定时刻,则不需要加天
                }
                dt = new DateTime(dt.Year, dt.Month, dt.Day, time.Hours, time.Minutes, time.Seconds);
                #endregion
                break;
            }

            return(dt);
        }
예제 #4
0
        /// <summary>
        /// 获取从现在起到下次执行时间剩余的时间
        /// </summary>
        /// <param name="strategy"></param>
        /// <returns></returns>
        public static TimeSpan GetNextTimeUp(TimerStrategy strategy)
        {
            //目标时间
            DateTime nextTime = GetNextDateTime(strategy);

            return(nextTime - DateTime.Now);
        }
예제 #5
0
        /// <summary>
        /// 开始一个新的任务计划
        /// </summary>
        /// <param name="scheduleName">计划名称</param>
        /// <param name="task">任务执行者</param>
        /// <param name="strategy">运行时间策略</param>
        /// <param name="Params">启动参数</param>
        /// <returns></returns>
        public static ITask StartSchedule(string scheduleName, ITask task, TimerStrategy strategy, params object[] Params)
        {
            if (Tasks.ContainsKey(scheduleName))
            {
                return(null);
            }

            task.Start(strategy, Params);
            Tasks.Add(scheduleName, task);

            return(task);
        }
예제 #6
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);
            }
        }
예제 #7
0
        /// <summary>
        /// 检测时间是否到了
        /// </summary>
        /// <param name="strategy"></param>
        /// <returns></returns>
        public static bool TimeIsUp(TimerStrategy strategy)
        {
            DateTime now = DateTime.Now;

            if (CheckTimeIsUp(strategy.StrategyByDay, now.TimeOfDay))
            {
                bool isUp = false;

                switch (strategy.TimerMode)
                {
                case TimerMode.OnTimes: isUp = strategy.OnTimes.Contains(now); break;

                case TimerMode.EveryDay: isUp = true; break;
                }

                return(isUp);
            }
            else
            {
                return(false);
            }
        }
예제 #8
0
        /// <summary>
        /// 从配置中读取并设置定时器值
        /// </summary>
        /// <param name="config"></param>
        /// <param name="node"></param>
        private void SetTimerConfigValue(TimerStrategy config, XmlNode node)
        {
            var datetimes = new List <DateTime>();
            var times     = new List <TimeSpan>();

            foreach (XmlNode xn in node.ChildNodes)
            {
                if (xn.NodeType == XmlNodeType.Element)
                {
                    var text = xn.InnerText;

                    switch (xn.Name.ToLower())
                    {
                    case "refname":
                        config.RefName = text;
                        break;

                    case "reentrant":
                        config.ReEntrant = string.Compare(text, "true", true) == 0;
                        break;

                    case "timermode":
                        if (!string.IsNullOrWhiteSpace(text))
                        {
                            config.TimerMode = (TimerMode)Enum.Parse(typeof(TimerMode), text);
                        }
                        break;

                    case "delay":
                        long delayTemp = 0;
                        long.TryParse(text, out delayTemp);
                        config.Delay = new TimeSpan(delayTemp * 10000L);
                        break;

                    case "interval":
                        long intervalTemp = 0;
                        long.TryParse(text, out intervalTemp);
                        if (config.StrategyByDay != null)
                        {
                            config.StrategyByDay.Interval = new TimeSpan(intervalTemp * 10000L);
                        }
                        else
                        {
                            config.Interval = new TimeSpan(intervalTemp * 10000L);
                        }
                        break;

                    case "times":
                        SetTimerConfigValue(config, xn);
                        break;

                    case "time":
                        var dt = DateTime.Parse(text);
                        datetimes.Add(dt);
                        break;

                    case "strategybyday":
                        //还是用这个函数处理下一级的配置
                        config.StrategyByDay = new TimerStrategyByDay();
                        SetTimerConfigValue(config, xn);        // 设置时间策略
                        break;

                    case "begintime":
                        if (String.IsNullOrEmpty(xn.InnerText))
                        {
                            xn.InnerText = "00:00:00";
                        }
                        config.StrategyByDay.BeginTime = TimeSpan.Parse(xn.InnerText);
                        break;

                    case "endtime":
                        if (String.IsNullOrEmpty(xn.InnerText))
                        {
                            xn.InnerText = "00:00:00";
                        }
                        config.StrategyByDay.EndTime = TimeSpan.Parse(xn.InnerText);
                        break;

                    case "timepoints":
                        SetTimerConfigValue(config, xn);        // 设置时间策略
                        break;

                    case "timevalue":
                        TimeSpan time = new TimeSpan(0);
                        if (TimeSpan.TryParse(text, out time))
                        {
                            times.Add(time);
                        }
                        break;
                    }
                }
            }

            if (times.Count != 0)
            {
                config.StrategyByDay.TimePoints = times.ToArray();
            }

            if (datetimes.Count != 0)
            {
                config.OnTimes = SortAndDistinct(datetimes);
            }
        }