/** * Builds the timer thread. * * @param scheduler * The owner scheduler. */ public TimerThread(Scheduler scheduler) { this.scheduler = scheduler; // Thread name. string name = "cron4j::scheduler[" + scheduler.getGuid() + "]::timer[" + guid + "]"; Thread = new Thread(new ThreadStart(Run)); Thread.Name = name; }
/** * Builds the launcher. * * @param scheduler * The owner scheduler. * @param collectors * Task collectors, used to retrieve registered tasks. * @param referenceTimeInMillis * A reference time for task launching. */ public LauncherThread(Scheduler scheduler, ITaskCollector[] collectors, long referenceTimeInMillis) { this.scheduler = scheduler; this.collectors = collectors; this.referenceTimeInMillis = referenceTimeInMillis; // Thread name. string name = "cron4j::scheduler[" + scheduler.getGuid() + "]::launcher[" + guid + "]"; Thread = new Thread(new ThreadStart(Run)); Thread.Name = name; }
static void Main(string[] args) { // var s1 = new SchedulingPattern("* * * * *"); var s = new Scheduler(); s.schedule("* * * * *", new Action<TaskExecutionContext>(x => Console.WriteLine(DateTime.Now))); s.schedule("*/2 * * * *", new Action<TaskExecutionContext>(x => Console.WriteLine("2-{0}",DateTime.Now))); s.schedule("*/3 * * * *", new Action<TaskExecutionContext>(x => Console.WriteLine("3-{0}", DateTime.Now))); s.start(); //var p1 = new Predictor(s1); //for (int i = 0; i < 100; i++) //{ // Console.WriteLine("{0:yyyy-MM-dd HH:mm}", p1.NextMatchingDate()); //} Console.ReadLine(); }
/** * Builds the executor. * * @param scheduler * The scheduler whose this executor belongs to. * @param task * The task that has to be executed. */ internal TaskExecutor(Scheduler scheduler, Task task) { this.scheduler = scheduler; this.task = task; this.context = new MyContext(this); this.myself = this; }
/** * Overrides {@link Thread#run()}. */ public void Run() { // What time is it? var millis = System.DateTime.Now; // Calculating next minute. var nextMinute = millis.AddMinutes(1).AddSeconds(-1 * millis.Second + 1); // Work until the scheduler is started. for (; ; ) { // Coffee break 'till next minute comes! var sleepTime = (nextMinute - System.DateTime.Now).TotalMilliseconds; if (sleepTime > 0) { try { SafeSleep(sleepTime); } catch (ThreadInterruptedException e) { // Must exit! break; } } // What time is it? millis = System.DateTime.Now; // Launching the launching thread! scheduler.spawnLauncher(millis.Ticks); // Calculating next minute. nextMinute = millis.AddMinutes(1); } // Discard scheduler reference. scheduler = null; }