Quartz Demo 任务调度程序简单Demo
创建Windows 控制台应用程序 , .net framework 版本 4.5.2
Nuget . Quartz 版本 用 2.5
using Quartz; using Quartz.Impl; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; /// <summary> /// 1. .Net Framework = 4.5.2 /// 2. Quartz = 2.5 /// </summary> namespace QuartzDemo { class Program { static void Main(string[] args) { //从工厂中获取一个调度器实例化 IScheduler scheduler = (IScheduler)StdSchedulerFactory.GetDefaultScheduler(); scheduler.Start(); //开启调度器 JobBuilder.Create(); IJobDetail job1 = JobBuilder.Create(Type.GetType("QuartzDemo.HelloJob")).WithIdentity("作业名称", "作业组").Build(); ITrigger trigger1 = TriggerBuilder.Create() .WithIdentity("触发器名称", "触发器组") .StartNow() //现在开始 .WithCronSchedule("0/5 * * * * ?").Build(); //触发时间,5秒一次。 //注册Listener , 所有Job都会起作用 var action = new Action<string>(Program.NotifyExecuteMessage); scheduler.ListenerManager.AddJobListener(new MyListenere(action)); scheduler.ScheduleJob(job1, trigger1); //把作业,触发器加入调度器。 } public static void NotifyExecuteMessage(string msg) { Console.WriteLine("任务执行完成 :" + msg); } } public class HelloJob : IJob { public void Execute(IJobExecutionContext context) { Console.WriteLine("作业执行JOB TEST!"); } } public class MyListenere : IJobListener { public Action<string> NotifyExecute; public MyListenere(Action<string> action) { this.NotifyExecute = action; } string IJobListener.Name { get { return "Myjob"; } } void IJobListener.JobExecutionVetoed(IJobExecutionContext context) { Console.WriteLine("MyJobListener.JobExecutionVetoed()"); } void IJobListener.JobToBeExecuted(IJobExecutionContext context) { Console.WriteLine("MyJobListener.jobToBeExecuted()"); } void IJobListener.JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException) { string msg = jobException == null ? context.JobDetail.JobType.FullName : jobException.Message; NotifyExecute(msg); } } }
分类:
C#
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律