Home  >  Article  >  Backend Development  >  Code graphic tutorial on how to use Quartz.Net under .net

Code graphic tutorial on how to use Quartz.Net under .net

黄舟
黄舟Original
2017-07-20 16:22:332996browse

This article mainly introduces in detail how to use Quartz.Net under .net, which has certain reference value. Interested friends can refer to it

Quartz.net is a job scheduling framework , the specific content is as follows

1. Add a reference to quartz.net in the project (managed here using nuget)

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


using Quartz;
using System;

namespace QuartzNetDemo
{
 /// <summary>
 /// 定时任务类
 /// </summary>
 public class TimingJob : IJob
 {
 public void Execute(IJobExecutionContext context)
 {
  //将要定时执行的逻辑代码写于此处
  Console.WriteLine("任务执行了");
 }
 }
}

In Program.cs: (Here is the console application Program)


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, information will be output every 2 seconds

Cron expression:

The cron expression in quartz.NET uses a 7-digit expression in the form "/5 * * ? * * *". The last digit is optional. The expression is from left to right, followed by seconds, minutes, hours, day of the month, month, day of the week, year

Special characters and explanations:

, or means.
/ a/b. a: represents the starting time, b frequency time.
* Frequency. That is, every fluctuation.
- Interval.
? Any value. That is, every fluctuation. Can only be used on DayofMonth and DayofWeek, they conflict. To specify one and the other, use ?
L to indicate the last one. Can only be used on DayofMonth and DayofWeek
W working days. means last. Can only be used on DayofWeek
#x#y. Only DayofMonth can be used. The yth week of a certain month x

The above is the detailed content of Code graphic tutorial on how to use Quartz.Net under .net. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn