示例#1
0
        public bool AddCron(string ckey, string cronex, DateTime?start, DateTime?end)
        {
            // no locking here as we won't touch any objects that are shared with another thread
            string[] cronflds = new string[6];
            Logr.Log(String.Format("AddCron: cronex({0}) start({1}) end({2})", cronex, start, end));
            try {
                if (m_CronMap.ContainsKey(ckey))
                {
                    // If there's already a timer with ckey it may be that an Excel users has triggered
                    // another invocation of s2cron( ) by editting the s2cfg sheet, or with a sh-ctrl-alt-F9.
                    // Either way, we need to remove the old timer, and create a new one, but only if the
                    // new one is different.
                    CronTimer oldTimer = m_CronMap[ckey];
                    if (oldTimer.Cronex == cronex && oldTimer.Start == start && oldTimer.End == end)
                    {
                        // no change, so we won't overwrite the entry for ckey
                        return(true);
                    }
                    m_CronMap.Remove(ckey);
                    oldTimer.Close( );
                }

                m_CronMap[ckey] = new CronTimer(ckey, cronex, start, end);
                return(true);
            }
            catch (Exception ex) {
                Logr.Log(String.Format("AddCron: {0}", ex.Message));
                return(false);
            }
        }
示例#2
0
        static void Main()
        {
            Console.WriteLine("Simple expression...");

            foreach (var value in CronExpression.Parse("0 0 0 ? JAN/2 6#2,6L")
                                                .GetAllTimesAfter(DateTimeOffset.Now)
                                                .Take(5))
            {
                Console.WriteLine(value);
            }

            Console.WriteLine();
            Console.WriteLine("Precise expression...");

            foreach (var value in CronExpression.Parse("* 0 0 0 0 0 1 JAN ?", CronFormat.Extended)
                                                .GetAllTimesAfter(DateTimeOffset.Now)
                                                .Take(10))
            {
                Console.WriteLine(value.ToString("O"/*ISO8601*/));
            }

            Console.WriteLine();
            Console.WriteLine("Simple enumeration...");

            var sw = new Stopwatch();
            sw.Start();

            foreach (var _ in CronExpression.Parse("0 0 0 * * * * * ?", CronFormat.Extended)
                                            .GetAllTimesAfter(DateTimeOffset.Now)
                                            .Take(100000))
            {
            }

            Console.WriteLine("100,000 times in {0} milliseconds.", sw.ElapsedMilliseconds);

            Console.WriteLine();
            Console.WriteLine("Timer...");

            using (var timer = new CronTimer(CronExpression.Parse("0 0 0/100 * * * * * ?", CronFormat.Extended)))
            {
                timer.Elapsed += (sender, e) => Console.WriteLine(e.Time.ToString("O"/*ISO8601*/));
                timer.Start();

                Thread.Sleep(TimeSpan.FromSeconds(1d));
            }

            Console.WriteLine();
            Console.WriteLine("Scheduler...");

            using (var scheduler = new Scheduler())
            {
                scheduler.Jobs.Add(new Job(
                    context => Console.WriteLine(context.Time.ToString("O"/*ISO8601*/)),
                    new CronTrigger("0 0 0/200 * * * * * ? *", CronFormat.Extended),
                    new CronTrigger("0 0 100/200 * * * * * ? *", CronFormat.Extended)));

                Thread.Sleep(TimeSpan.FromSeconds(1d));
            }

            Console.WriteLine();
            Console.WriteLine("Press any key to quit.");

            Console.ReadKey(true);
        }