首先下载 quartznet-2.0.0,在官网下载,这个版本需要.net 3.5以上,2.0的不能用
打开这个解决方案后,编译
编译后,只需要他的三个dll
接下来 创建自己的代码,创建一个工程
例子1.用代码方式触发
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using System; using System.Collections.Generic; using System.Text; using Quartz; using Quartz.Impl; namespace ConsoleApplication1 { class Program { private static MothodFun m; static void Main(string[] args) { ISchedulerFactory sf = new StdSchedulerFactory(); IScheduler scheduler = sf.GetScheduler(); //创建调度实例 //创建任务实例 IJobDetail job = JobBuilder.Create<TimingJob>().WithIdentity(new JobKey("job1")).Build(); //创建触发器实例 ITrigger trigger = TriggerBuilder.Create().StartAt(DateTime.Now.AddSeconds(0)).WithCronSchedule("0/1 * * ? * * ").Build(); job.JobDataMap.Add("fun", new Action(Test)); m = new MothodFun(); job.JobDataMap.Add("MothodFun", m); scheduler.ScheduleJob(job, trigger); //绑定触发器和任务 scheduler.Start(); //启动监控 } public static void Test() { Console.WriteLine("这是回调的Test" + "\t" + m.Count.ToString()); } } [DisallowConcurrentExecution] public class TimingJob : IJob { public void Execute(IJobExecutionContext context) { //将要定时执行的逻辑代码写于此处 Console.WriteLine(DateTime.Now.ToString() + "\tTimingJob1工作太久了,休息10分钟吧。"); JobDataMap data = context.JobDetail.JobDataMap; Action action = data.Get("fun") as Action; if (action != null) action(); MothodFun m = data.Get("MothodFun") as MothodFun; if (m != null) { m.Fun1(); m.Fun2(); m.Fun3(); m.Count++; } } } public class MothodFun { public int Count { get; set; } public void Fun1() { Console.WriteLine("这是方法1"); } public void Fun2() { Console.WriteLine("这是方法2"); } public void Fun3() { Console.WriteLine("这是方法3"); } } }
例子2,用配置文件的方式
quartz.config内容
# You can configure your scheduler in either <quartz> configuration section
# or in quartz properties file
# Configuration section has precedence
quartz.scheduler.instanceName = ServerScheduler
# configure thread pool info
quartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, Quartz
quartz.threadPool.threadCount = 10
quartz.threadPool.threadPriority = Normal
#--------------------------------*************plugin配置------------------------------------
# job initialization plugin handles our xml reading, without it defaults are used
quartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz
quartz.plugin.xml.fileNames = ~/quartz_jobs.xml
# export this server to remoting context
quartz.scheduler.exporter.type = Quartz.Simpl.RemotingSchedulerExporter, Quartz
quartz.scheduler.exporter.port = 555
quartz.scheduler.exporter.bindName = QuartzScheduler
quartz.scheduler.exporter.channelType = tcp
quartz.scheduler.exporter.channelName = httpQuartz
quartz_jobs.xml 文件内容
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<?xml version="1.0" encoding="UTF-8"?> <!-- This file contains job definitions in schema version 2.0 format --> <job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"> <processing-directives> <overwrite-existing-data>true</overwrite-existing-data> </processing-directives> <schedule> <!--定义示例任务1 Job--> <job> <name>Job1</name> <group>JobGroup</group> <description>Quartz示例任务1</description> <job-type>QuartzTest.TestJob,QuartzTest</job-type> <durable>true</durable> <recover>false</recover> </job> <trigger> <simple> <name>sampleSimpleTrigger</name> <group>sampleSimpleTriggerGroup</group> <description>Simple trigger to simply fire sample job</description> <job-name>Job1</job-name> <job-group>JobGroup</job-group> <misfire-instruction>SmartPolicy</misfire-instruction> <repeat-count>-1</repeat-count> <repeat-interval>1000</repeat-interval> </simple> </trigger> <job> <name>Job2</name> <group>JobGroup2</group> <description>Quartz示例任务1</description> <job-type>QuartzTest.Job2,QuartzTest</job-type> <durable>true</durable> <recover>false</recover> </job> <trigger> <simple> <name>sampleSimpleTrigger2</name> <group>sampleSimpleTriggerGroup2</group> <description>Simple trigger to simply fire sample job</description> <job-name>Job2</job-name> <job-group>JobGroup2</job-group> <misfire-instruction>SmartPolicy</misfire-instruction> <repeat-count>-1</repeat-count> <repeat-interval>50</repeat-interval> </simple> </trigger> </schedule> </job-scheduling-data>
代码下载
https://files.cnblogs.com/files/baihong/quartz2.0%E5%AD%A6%E4%B9%A0%E4%BE%8B%E5%AD%90.rar