How to use quartz. Net under. Net

Time:2021-6-10

Quartz.net is a job scheduling framework. The details are as follows

1. Add the reference of quartz.net to the project (nuget management is used here)

Create a new class, timingjob, which is mainly used to implement task logic

using Quartz;
using System;

namespace QuartzNetDemo
{
 /// <summary>
 ///Timing task class
 /// </summary>
 public class TimingJob : IJob
 {
 public void Execute(IJobExecutionContext context)
 {
  //Write the logic code to be executed regularly here
  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, after 5 seconds, output information every 2 seconds

Cron expression:

Cron expressions in quartz.net use the form of “/ 5 * *? * *”   For such a 7-bit expression, the last year is not required. The expression is from left to right, which is the second, minute, hour, month, day, month, week and year

Special characters and interpretation:

The meaning of or.
/   a/b。 a: Represents the start time, B frequency time.
*   Frequency. That is, every fluctuation.
–   section.
?   Any value. That is, every fluctuation. It can only be used in dayof month and dayof week. They conflict. Specify one to use?
L   Last. It can only be used in dayof month and dayof week
W   working day. Last. It can only be used in dayof week
#x#y。 Only dayof month. Week y X of a month

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.