標籤:creat 調度 stat mon 代碼 特殊字元 tde cin 某月
Quartz.net是作業調度架構
1. 項目中添加quartz.net的引用(這裡使用nuget管理)
建立一個類TimingJob,該類主要用於實現任務邏輯
using Quartz;using System;namespace QuartzNetDemo{ /// <summary> /// 定時任務類 /// </summary> public class TimingJob : IJob { public void Execute(IJobExecutionContext context) { //將要定時執行的邏輯代碼寫於此處 Console.WriteLine("任務執行了"); } }}
在Program.cs中:(這裡是控制台應用程式)
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(); } }}
程式運行時,經過5秒後,每間隔2秒輸出資訊
Cron運算式:
quartz.NET中的cron運算式使用形如 "/5 * * ? * * *" 這樣的7位運算式,最後一位年非必選,運算式從左至右,依此是秒、分、時、月第幾天、月、周幾、年
| 名稱 |
範圍 |
可使用的特殊字元 |
| 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 |
空, 1970-2099 |
, - * / |
特殊字元與解釋:
, 或的意思。
/a/b。 a:代表起始時間,b頻率時間。
*頻率。 即每一次波動。
-區間。
?任意值 。 即每一次波動。只能用在DayofMonth和DayofWeek,二者衝突。指定一個另一個一個要用?
L表示最後。 只能用在DayofMonth和DayofWeek
W工作日。 表示最後。 只能用在DayofWeek
#x#y。 只能用DayofMonth。 某月的第y個星期x
【Quartz.Net】.net 下使用Quartz.Net