Basic Code Sample

This sample demonstrations the basics for getting a task scheduled and running by scheduling a task to execute one minute from program execution.

    class Program {
        static void Main(string[] args) {
            //
            // Init... more ideally, this should be injected
            var taskRepository = new MemoryTaskRepository(); 
            var np = new NotificationProvider();                         
            var rtm = new RunTimeManager(np);                       
            var stm = new ScheduleManager(taskRepository, rtm, np);  
            stm.Start();
            Console.WriteLine("Scheduler Start Time: {0}", DateTime.Now);
            //
            // schedule something
            var taskName = "My Task";
            var oneMinuteFromNow = DateTime.Now.AddMinutes(1.0d);
            var cronSchedule = string.Format(
                "{0} {1} * * *", oneMinuteFromNow.Minute, oneMinuteFromNow.Hour);
            Console.WriteLine("cron string: {0}", cronSchedule);
            var def = stm.AddTaskDefinition(cronSchedule, new TaskDefinition {
                TaskType = typeof(MyTask),
                UniqueID = Guid.NewGuid().ToString(),
                TaskName = taskName
            });
            Console.WriteLine("Scheduled {0}.  Next run time: {1}", 
                taskName, stm.GetNextScheduledTime(DateTime.Now, def));
            //
            // wait for results
            Console.WriteLine("Press <ENTER> to quit....");
            Console.ReadLine();
            stm.Stop();

        }
    }

    public class MyTask : StandardTask {

        public override bool Execute(Dictionary<string, object> customData) {
            Console.WriteLine("Hello from MyTask");
            return true;
        }
    }

Last edited Dec 19, 2011 at 9:25 PM by kcourville, version 9

Comments

No comments yet.