0

I am new to quartz.net. I want to build a simple window based application which scheduled the task. Suppose I have 4 task ans it start and end time Example

Breakfast ; 8:00;8:30
Lunch;13:00;13:30
dinner;19:30;20:00

Now I want when I click on button at 8:00 AM a message box should appear with a text "breakfast started!!!" at 8:30 AM again a message box should appear with a text as "Breakfast end!!!" and so on.

I had gone through tutorial. But confuse how to proceed. Can any one help me out?

EDIT

Is it possible to use one job and one triggers for this?

IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();


                for (int i = 0; i < listBox1.Items.Count; i++)
                {
                    string[] strArr = Regex.Split(listBox1.Items[i].ToString(), @";", RegexOptions.Multiline);
                    // define the job and tie it to our HelloJob class
                    IJobDetail jobStart = JobBuilder.Create<HelloJob>()
        .WithIdentity("job" + i, "group1") // name "myJob", group "group1"
        .StoreDurably()
        .UsingJobData("jobSays", strArr[0].ToUpper().Trim() + " " + "starts")
        .Build();

                    string[] ArrStart = strArr[1].Trim().Split(':');

                    ITrigger triggerstart = TriggerBuilder.Create()
                        .WithIdentity("trigger" + i, "group1")

.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(Convert.ToInt32(ArrStart[0]), Convert.ToInt32(ArrStart[1])))
                        // .WithSimpleSchedule(x => x.WithMisfireHandlingInstructionIgnoreMisfires()


                                    .ForJob(jobStart)
                                .Build();
                    // .WithSchedule(CronScheduleBuilder.CronSchedule("0 4 06 1/1 * ? *"))

                    // Tell quartz to schedule the job using our trigger
                    scheduler.ScheduleJob(jobStart, triggerstart);
                    scheduler.Start();
                    string[] Arrend = strArr[2].Trim().Split(':');
                    IJobDetail jobend = JobBuilder.Create<HelloJob>()
        .WithIdentity("job1" + i, "group1")
        .StoreDurably()
        .UsingJobData("jobSays", strArr[0].ToUpper().Trim() + " " + "end")
        .Build();

                    ITrigger triggerend = TriggerBuilder.Create()
                        .WithIdentity("trigger1" + i, "group1")

.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(Convert.ToInt32(Arrend[0]), Convert.ToInt32(Arrend[1])))
                        // .WithSimpleSchedule(x => x.WithMisfireHandlingInstructionIgnoreMisfires()


                                    .ForJob(jobend)
                                .Build();


                    scheduler.ScheduleJob(jobend, triggerend);
                    scheduler.Start();
                }


            }
            catch (SchedulerException se)
            {
                Console.WriteLine("Scheduler Exception : " + se);
            }
        }
        public class HelloJob : IJob
        {
            public void Execute(IJobExecutionContext context)
            {
                JobKey key = context.JobDetail.Key;
                JobDataMap dataMap = context.JobDetail.JobDataMap;
                string jobSays = dataMap.GetString("jobSays");
                MessageBox.Show(jobSays);
            }
        }
Pritesh Chhunchha
  • 179
  • 1
  • 2
  • 15
  • You can use one job, but you need multiple triggers. – Rabban Dec 07 '16 at 09:57
  • @Rabban ok. but if we want to pass any parameter for that particular tigger than how would it be done like in my above example I am pass a task name to the and get at the the time of Execute method – Pritesh Chhunchha Dec 07 '16 at 10:05

1 Answers1

0

You can schedule a job as follows and use this simple example:

IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();

        IJobDetail job = JobBuilder.Create<MealAlertJob>()
            .WithIdentity("mealJob", "mealJobGroup")
            .Build();

        ITrigger trigger = TriggerBuilder.Create()
                    .WithIdentity("breakfastStartTrigger", "mealTriggerGroup")
                    .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(8, 0))
                    .ForJob(job)
                    .Build();

        ITrigger trigger1 = TriggerBuilder.Create()
                    .WithIdentity("breakfastEndTrigger", "mealTriggerGroup")
                    .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(8, 30))
                    .ForJob(job)
                    .Build();


        scheduler.ScheduleJob(job, trigger);
        scheduler.ScheduleJob(trigger1);

        scheduler.Start();

You should implement your logic (let's say showing a message box) in your job class's "Execute" method. Defining multiple triggers for your job results in multiple execution. In the job class for example "MealAlertJob" you may check the time and show an alert based on it. In the above example you can add more triggers for the other four times (lunch start/end and dinner start/end).

public class MealAlertJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        var now = DateTime.Now;
        var hour = now.Hour;
        var minute = now.Minute;

        if (hour == 8 && minute == 0)
            System.Windows.Forms.MessageBox.Show("Breakfast started!!!");

        //and so on....
    }
}

or something like this.

aminexplo
  • 360
  • 1
  • 13
  • Suppose if time are not known then..If its depend on user's input the? – Pritesh Chhunchha Dec 07 '16 at 13:03
  • @PriteshChhunchha Once you receive the times from user you can store it somewhere like a database or a config file. You can restore the times from the place you saved them one time when application starts. then decide based on them in Execute method. – aminexplo Dec 07 '16 at 13:12