Exemple #1
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)
        {
            ServiceConfig = new ServiceTimerConfig();
            var config = new List <TimerConfig>();

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

                    case "config":
                        var tmp = new TimerConfig();
                        SetTimerConfigValue(tmp, node);
                        config.Add(tmp);
                        break;
                    }
                }
            }
            ServiceConfig.Config = config.ToArray();

            return(ServiceConfig);
        }
Exemple #2
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="config">配置参数</param>
        public TimerControl(TimerConfig config)
        {
            Config = config;
            if (Config == null)
            {
                throw new Exception("定时器时间配置异常!");
            }

            switch (Config.TimerMode)
            {
            case TimerMode.Date:
                if (Config.MonthSeq < 1 || Config.MonthSeq > 12)
                {
                    throw new Exception("定时器时间配置异常(月份取值只能是1~12)!");
                }
                var dt      = new DateTime(2012, Config.MonthSeq, 1); // 之所以选2012,是因为他是闰年,因此2月有29天。
                var lastDay = GetLastDayByMonth(dt);
                if (Config.DaySeq < 1 || Config.DaySeq > lastDay)
                {
                    throw new Exception("定时器时间配置异常(" + Config.MonthSeq + "月份的天数取值只能是1~" + lastDay + ")!");
                }
                break;

            case TimerMode.Day: break;

            case TimerMode.Month:
                if (Config.DaySeq < 1 || Config.DaySeq > 31)
                {
                    throw new Exception("定时器时间配置异常(天数取值只能是1~31)!");
                }
                break;

            case TimerMode.Week:
                if (Config.DaySeq < 0 || Config.DaySeq > 6)
                {
                    throw new Exception("定时器时间配置异常(星期取值只能是0~6)!");
                }
                break;

            case TimerMode.LastDayOfMonth:
                if (Config.DaySeq != 0)
                {
                    // 如果等于0的话,表示是每个月的最后一天。
                    if (Config.DaySeq < 1 || Config.DaySeq > 28)
                    {
                        throw new Exception("定时器时间配置异常(倒数的天数只能是1~28,即倒数的第1天,第2天。。。有些月份并没有29.30.31天,因此最大只允许倒数第28天)!");
                    }
                    Config.DaySeq -= 1;
                }
                break;

            case TimerMode.Year:
                if (Config.DaySeq < 1 || Config.DaySeq > 366)
                {
                    throw new Exception("定时器时间配置异常(天数取值只能是1~366)!");
                }
                break;
            }
        }
Exemple #3
0
        /// <summary>
        /// 设置定时器值
        /// </summary>
        /// <param name="config"></param>
        /// <param name="node"></param>
        private void SetTimerConfigValue(TimerConfig config, XmlNode node)
        {
            var times = new List <TimeSpan>();

            foreach (XmlNode xn in node.ChildNodes)
            {
                if (xn.NodeType == XmlNodeType.Element)
                {
                    int  tmp;
                    long longTmp;
                    switch (xn.Name.ToLower())
                    {
                    case "refname":
                        config.RefName = xn.InnerText;
                        break;

                    case "timermode":
                        config.TimerMode = (TimerMode)Enum.Parse(typeof(TimerMode), xn.InnerText);
                        break;

                    case "delay":
                        Int64.TryParse(xn.InnerText, out longTmp);
                        config.Delay = new TimeSpan(longTmp * 10 * 1000L);        // Delay配置值为毫秒
                        break;

                    case "interval":
                        Int64.TryParse(xn.InnerText, out longTmp);            // Interval配置值为毫秒
                        config.Interval = new TimeSpan(longTmp * 10 * 1000L);
                        break;

                    case "monthseq":        // 月份
                        Int32.TryParse(xn.InnerText, out tmp);
                        config.MonthSeq = tmp;
                        break;

                    case "dayseq":          // 指定第几天的序号
                        Int32.TryParse(xn.InnerText, out tmp);
                        config.DaySeq = tmp;
                        break;

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

                    case "timevalue":
                        var mc = RegEx.Match(xn.InnerText);
                        if (!mc.Success)
                        {
                            throw new Exception("时间配置不正确!");
                        }
                        int h;
                        Int32.TryParse(mc.Groups["h"].Value, out h);
                        int m;
                        Int32.TryParse(mc.Groups["m"].Value, out m);
                        int s;
                        Int32.TryParse(mc.Groups["s"].Value, out s);
                        times.Add(new TimeSpan(h, m, s));
                        break;
                    }
                }
            }
            if (times.Count != 0)
            {
                config.Times = times.ToArray();
            }
        }