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.運行就能看到效果