using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz;
using Quartz.Impl;
namespace QuartzTest
{
class Program
{
static void Main(string[] args)
{
RunProgram();
Console.ReadKey();
}
private static async Task RunProgram()
{
// 1、实例化调度器工厂
StdSchedulerFactory factory = new StdSchedulerFactory();
//实例化调度器
IScheduler scheduler = await factory.GetScheduler();
//2、创建一个任务
IJobDetail job = JobBuilder.Create<TimeJob>().WithIdentity("job1", "group1").Build();
//3、创建一个触发器
ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.WithCronSchedule("0/5 * * * * ?") //5秒执行一次
.Build();
//4、将任务与触发器添加到调度器中
await scheduler.ScheduleJob(job, trigger);
//5、开始执行
await scheduler.Start();
}
public class TimeJob : IJob
{
public async Task Execute(IJobExecutionContext context)
{
await Console.Out.WriteLineAsync("Hello QuartzNet..." + DateTime.Now + Environment.NewLine);
}
}
}
}
献给诸如小白的我,Quartz 小Demo ,C# 打上Quartz包就能运行
最新推荐文章于 2024-08-01 03:55:25 发布