'C# How to fire routine by Quartz.net every day at specific time of day
i am not before dev pc but i got a code which seems like work.
private void QuartzTest_Load(object sender, EventArgs e)
{
// construct a scheduler factory
ISchedulerFactory schedFact = new StdSchedulerFactory();
// get a scheduler
IScheduler sched = schedFact.GetScheduler();
sched.Start();
IJobDetail job = JobBuilder.Create<LoggingJob>()
.WithIdentity("myJob", "group1")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithDailyTimeIntervalSchedule
(s =>
s.WithIntervalInHours(24)
.OnEveryDay()
.StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(08, 00))
)
.Build();
sched.ScheduleJob(job, trigger);
}
it seems the trigger will trigger job every day at 8:00 AM every morning. anyone can confirm that really does the above code will trigger my routine every day at 8:00 AM every morning ?
i asked this kind of question because i am not before dev pc so i can not test the above code that it will work fine or not?
also i am curious to know how could i exclude Saturday and Sunday as a result my routine will not be fire. where to add day name no to fire my routine. only my routine should be fire from Monday to Friday.
thanks
Solution 1:[1]
To do it only on weekdays you can use a cron expression like this
0 0 8 ? * MON-FRI *
Usage is described here
Would look like this for you
ITrigger trigger = TriggerBuilder.Create()
.WithCronSchedule("0 0 8 ? * MON-FRI *")
.Build();
This should fire every day at 8am except for weekend
You can use this website to generate your cron expressions: http://www.cronmaker.com/
Solution 2:[2]
If you want to run it on weekdays without using a cron expression, you can also use:
var trigger = TriggerBuilder
.Create()
.WithSchedule(CronScheduleBuilder
.AtHourAndMinuteOnGivenDaysOfWeek(8,
0,
DayOfWeek.Monday,
DayOfWeek.Tuesday,
DayOfWeek.Wednesday,
DayOfWeek.Thursday,
DayOfWeek.Friday))
.Build();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 |