当前位置: 首页>>代码示例>>C#>>正文


C# CronExpression.Parse方法代码示例

本文整理汇总了C#中CronExpression.Parse方法的典型用法代码示例。如果您正苦于以下问题:C# CronExpression.Parse方法的具体用法?C# CronExpression.Parse怎么用?C# CronExpression.Parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CronExpression的用法示例。


在下文中一共展示了CronExpression.Parse方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: FromCronFormat

        /// <summary>
        /// Creates a trigger using a cron string.
        /// </summary>
        /// <param name="cronString">String using cron defined syntax for specifying a time interval. See remarks for syntax.</param>
        /// <returns>Array of <see cref="Trigger" /> representing the specified cron string.</returns>
        /// <exception cref="System.NotImplementedException">Unsupported cron string.</exception>
        /// <remarks>
        ///   <para>NOTE: This method does not support all combinations of cron strings. Please test extensively before use. Please post an issue with any syntax that should work, but doesn't.</para>
        ///   <para>Currently the cronString only supports numbers and not any of the weekday or month strings. Please use numeric equivalent.</para>
        ///   <para>This section borrows liberally from the site http://www.nncron.ru/help/EN/working/cron-format.htm. The cron format consists of five fields separated by white spaces:</para>
        ///   <code>
        ///   &lt;Minute&gt; &lt;Hour&gt; &lt;Day_of_the_Month&gt; &lt;Month_of_the_Year&gt; &lt;Day_of_the_Week&gt;
        ///   </code>
        ///   <para>Each item has bounds as defined by the following:</para>
        ///   <code>
        ///   * * * * *
        ///   | | | | |
        ///   | | | | +---- Day of the Week   (range: 1-7, 1 standing for Monday)
        ///   | | | +------ Month of the Year (range: 1-12)
        ///   | | +-------- Day of the Month  (range: 1-31)
        ///   | +---------- Hour              (range: 0-23)
        ///   +------------ Minute            (range: 0-59)
        ///   </code>
        ///   <para>Any of these 5 fields may be an asterisk (*). This would mean the entire range of possible values, i.e. each minute, each hour, etc.</para>
        ///   <para>Any of the first 4 fields can be a question mark ("?"). It stands for the current time, i.e. when a field is processed, the current time will be substituted for the question mark: minutes for Minute field, hour for Hour field, day of the month for Day of month field and month for Month field.</para>
        ///   <para>Any field may contain a list of values separated by commas, (e.g. 1,3,7) or a range of values (two integers separated by a hyphen, e.g. 1-5).</para>
        ///   <para>After an asterisk (*) or a range of values, you can use character / to specify that values are repeated over and over with a certain interval between them. For example, you can write "0-23/2" in Hour field to specify that some action should be performed every two hours (it will have the same effect as "0,2,4,6,8,10,12,14,16,18,20,22"); value "*/4" in Minute field means that the action should be performed every 4 minutes, "1-30/3"  means the same as "1,4,7,10,13,16,19,22,25,28".</para>
        /// </remarks>
        public static Trigger[] FromCronFormat(string cronString)
        {
            CronExpression cron = new CronExpression();
            cron.Parse(cronString);

            // TODO: Figure out all the permutations of expression and convert to Trigger(s)
            /* Time (fields 1-4 have single number and dow = *)
             * Time repeating
             * Daily
             * Weekly
             * Monthly
             * Monthly DOW
             */

            List<Trigger> ret = new List<Trigger>();

            // MonthlyDOWTrigger
            if (!cron.DOW.IsEvery)
            {
                // Determine DOW
                DaysOfTheWeek dow = 0;
                if (cron.DOW.vals.Length == 0)
                    dow = DaysOfTheWeek.AllDays;
                else if (cron.DOW.range)
                    for (int i = cron.DOW.vals[0]; i <= cron.DOW.vals[1]; i += cron.DOW.step)
                        dow |= (DaysOfTheWeek)(1 << (i - 1));
                else
                    for (int i = 0; i < cron.DOW.vals.Length; i++)
                        dow |= (DaysOfTheWeek)(1 << (cron.DOW.vals[i] - 1));

                // Determine months
                MonthsOfTheYear moy = 0;
                if ((cron.Months.vals.Length == 0 || (cron.Months.vals.Length == 1 && cron.Months.vals[0] == 1)) && cron.Months.IsEvery)
                    moy = MonthsOfTheYear.AllMonths;
                else if (cron.Months.range)
                    for (int i = cron.Months.vals[0]; i <= cron.Months.vals[1]; i += cron.Months.step)
                        moy |= (MonthsOfTheYear)(1 << (i - 1));
                else
                    for (int i = 0; i < cron.Months.vals.Length; i++)
                        moy |= (MonthsOfTheYear)(1 << (cron.Months.vals[i] - 1));

                Trigger tr = new MonthlyDOWTrigger(dow, moy, WhichWeek.AllWeeks);
                ret.AddRange(ProcessCronTimes(cron, tr));
            }
            // MonthlyTrigger
            else if (cron.Days.vals.Length > 0)
            {
                // Determine DOW
                List<int> days = new List<int>();
                if (cron.Days.range)
                    for (int i = cron.Days.vals[0]; i <= cron.Days.vals[1]; i += cron.Days.step)
                        days.Add(i);
                else
                    for (int i = 0; i < cron.Days.vals.Length; i++)
                        days.Add(cron.Days.vals[i]);

                // Determine months
                MonthsOfTheYear moy = 0;
                if ((cron.Months.vals.Length == 0 || (cron.Months.vals.Length == 1 && cron.Months.vals[0] == 1)) && cron.Months.IsEvery)
                    moy = MonthsOfTheYear.AllMonths;
                else if (cron.Months.range)
                    for (int i = cron.Months.vals[0]; i <= cron.Months.vals[1]; i += cron.Months.step)
                        moy |= (MonthsOfTheYear)(1 << (i - 1));
                else
                    for (int i = 0; i < cron.Months.vals.Length; i++)
                        moy |= (MonthsOfTheYear)(1 << (cron.Months.vals[i] - 1));

                Trigger tr = new MonthlyTrigger(1, moy) { DaysOfMonth = days.ToArray() };
                ret.AddRange(ProcessCronTimes(cron, tr));
            }
            // DailyTrigger
            else if (cron.Months.IsEvery && cron.DOW.IsEvery && cron.Days.repeating)
//.........这里部分代码省略.........
开发者ID:tablesmit,项目名称:task-scheduler-managed-wrapper,代码行数:101,代码来源:TaskServiceCronExt.cs


注:本文中的CronExpression.Parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。