Quartz的定时
添加引用quartz.dll
把下面这句话添加到程序开始
IScheduler sched;
ISchedulerFactory sf = new StdSchedulerFactory();
sched = sf.GetScheduler();
JobDetail job = new JobDetail("job1", "group1", typeof(TestJob));//IndexJob为实现了IJob接口的类
DateTime ts = TriggerUtils.GetNextGivenSecondDate(null, 5);//5秒后开始第一次运行
//TimeSpan interval = TimeSpan.FromHours(1);//每隔1小时执行一次
// TimeSpan interval = TimeSpan.FromSeconds(5);
//Trigger trigger = new SimpleTrigger("trigger1", "group1", "job1", "group1", ts, null,
// SimpleTrigger.RepeatIndefinitely, interval);//每若干小时运行一次,小时间隔由appsettings中的IndexIntervalHour参数指定
Trigger trigger = TriggerUtils.MakeDailyTrigger("tigger1",9, 46);
trigger.JobName = "job1";
trigger.JobGroup = "group1";
trigger.Group = "group1";
sched.AddJob(job, true);
sched.ScheduleJob(trigger);
sched.Start();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
sched.Shutdown(true);
TestJob是继承Ijob实现这个类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
using System.Windows.Forms;
namespace 测试定时任务
{
class TestJob:IJob
{
#region IJob 成员
public void Execute(JobExecutionContext context)
{
MessageBox.Show("任务执行啦!!!");
}
#endregion
}
}
testjob是你要定时执行的任务在这个方法中写