Use Quartz. net in. Net, and use quartz.net in. net.
Quartz.net is a job scheduling framework.
1. Add quartz.net references to the project (nuget management is used here)
Create a new class TimingJob, which is mainly used to implement the task Logic
Using Quartz; using System; namespace QuartzNetDemo {// <summary> // scheduled task class /// </summary> public class TimingJob: IJob {public void Execute (IJobExecutionContext context) {// write the logic code to be periodically executed on the Console. writeLine ("task executed ");}}}
In Program. cs: (here is the console application)
using Quartz;using Quartz.Impl;using System;namespace QuartzNetDemo{ class Program { static void Main(string[] args) { ISchedulerFactory sf = new StdSchedulerFactory(); IScheduler scheduler = sf.GetScheduler(); IJobDetail job = JobBuilder.Create<TimingJob>().WithIdentity("job1", "mygroup").Build(); ITrigger trigger = TriggerBuilder.Create().StartAt(DateTime.Now.AddSeconds(5)).WithCronSchedule("/2 * * ? * *").Build(); scheduler.ScheduleJob(job,trigger); scheduler.Start(); } }}
When the program is running, information is output every 2 seconds after 5 seconds
Cron expression:
The cron expression in quartz. NET is like "/5 **? * ** "This is a 7-bit expression. The last year is optional. The expression is left to right, the second, minute, hour, day of the month, month, day of the week, and year.
Name |
Range |
Special characters that can be used |
Seconds |
0-59 |
,-*/ |
Minutes |
0-59 |
,-*/ |
Hours |
0-23 |
,-*/ |
Day of month |
1-31 |
,-*? /L W |
Month |
1-12 or JAN-DEC |
,-*/ |
Day of week |
1-7 or SUN-SAT |
,-*? /L # |
Year |
Null, 1970-2099 |
,-*/ |
Special characters and explanations:
, Or.
/A/B. A: Start Time and B frequency.
* Frequency. That is, every fluctuation.
-Interval.
? Any value. That is, every fluctuation. It can only be used in DayofMonth and DayofWeek. Specify one for another?
L indicates the end. It can only be used in DayofMonth and DayofWeek
W working days. Indicates the end. It can only be used in DayofWeek
# X # y. Only DayofMonth can be used. Week y of a month x