1.添加引用,我这里哟那的是3.0.7版本:
2.直接上代码:
private static readonly string tiggerName = "TestJobTrigger"; private static readonly string gropName = "TestJobTriggerGrop"; private static readonly string jobName = "TestJob"; private static readonly string tiggerName1 = "MicrovuJobTrigger"; private static readonly string gropName1 = "MicrovuJobTriggerGrop"; private static readonly string jobName1 = "MicrovuJob"; //从工厂中获取一个调度器实例化 private static IScheduler scheduler = null; private void button1_Click(object sender, EventArgs e) { Console.WriteLine("开始任务...."); if (textBox1.Text!="") { TestJobStart(); } else { MicrovuJobStart(); } } private static async void TestJobStart() { //从工厂中获取一个调度器实例化 scheduler = await StdSchedulerFactory.GetDefaultScheduler(); await scheduler.Start(); //创建一个作业 IJobDetail job1 = JobBuilder.Create<TestJob>() .WithIdentity(jobName, gropName) .UsingJobData("key", "value")// 传递参数 在Execute方法中获取(以什么类型值传入,取值就用相应的类型方法取值) .UsingJobData("key1", "value1") .Build(); // 创建触发器 ITrigger trigger1 = TriggerBuilder.Create() .WithIdentity(tiggerName, gropName) .StartNow() //现在开始 .WithSimpleSchedule(x => x //触发时间,10秒一次。 .WithIntervalInSeconds(5) .RepeatForever()) //不间断重复执行 .Build(); await scheduler.ScheduleJob(job1, trigger1); //把作业,触发器加入调度器。 // 清除任务和触发器 //ClearJobTrigger(); } private static async void MicrovuJobStart() { //从工厂中获取一个调度器实例化 scheduler = await StdSchedulerFactory.GetDefaultScheduler(); await scheduler.Start(); //创建一个作业 IJobDetail job1 = JobBuilder.Create<MicrovuJob>() .WithIdentity(jobName1, gropName1) .UsingJobData("key", "MicrovuJob")// 传递参数 在Execute方法中获取(以什么类型值传入,取值就用相应的类型方法取值) .UsingJobData("key1", "MicrovuJob1") .Build(); // 创建触发器 ITrigger trigger1 = TriggerBuilder.Create() .WithIdentity(tiggerName1, gropName1) .StartNow() //现在开始 .WithSimpleSchedule(x => x //触发时间,10秒一次。 .WithIntervalInSeconds(10) .RepeatForever()) //不间断重复执行 .Build(); await scheduler.ScheduleJob(job1, trigger1); //把作业,触发器加入调度器。 // 清除任务和触发器 //ClearJobTrigger(); } /// <summary> /// 清除任务和触发器 /// </summary> private static void ClearJobTrigger() { TriggerKey triggerKey = new TriggerKey(tiggerName, gropName); JobKey jobKey = new JobKey(jobName, gropName); if (scheduler != null) { scheduler.PauseTrigger(triggerKey); scheduler.UnscheduleJob(triggerKey); scheduler.DeleteJob(jobKey); scheduler.Shutdown();// 关闭 } }
3.具体的Job,要执行的工作写在这里:
public class TestJob : IJob { /// <summary> /// 测试作业 /// </summary> /// <param name="context"></param> /// <returns></returns> public Task Execute(IJobExecutionContext context) { return Task.Run(() => { JobDataMap dataMap = context.JobDetail.JobDataMap; string k = dataMap.GetString("key");//获取参数(可根据传递的类型使用GetInt、GetFloat、GetString.....) string k1 = dataMap.GetString("key1"); }); } } public class MicrovuJob : IJob { /// <summary> /// 测试作业 /// </summary> /// <param name="context"></param> /// <returns></returns> public Task Execute(IJobExecutionContext context) { return Task.Run(() => { JobDataMap dataMap = context.JobDetail.JobDataMap; string k = dataMap.GetString("key");//获取参数(可根据传递的类型使用GetInt、GetFloat、GetString.....) string k1 = dataMap.GetString("key1"); }); } }
4.运行就能看到效果