首先要添加Quartz.dll 类库的应用,下面的都都要拷贝到项目当中
using System;
using System.Collections.Generic;using System.Linq;
using System.Text;
using Quartz;
using Quartz.Impl;
using Quartz.Impl.Triggers;
using System.Timers;
using System.Threading;
using System.Configuration;
namespace SchedualTest
{
class Program
{
static void Main(string[] args)
{
//获取配置文件中的时间
//时间格式设置讲解 :http://blog.csdn.net/lzq_it/article/details/7833726
string time = ConfigurationSettings.AppSettings["time"];ISchedulerFactory scheduleFactory = new StdSchedulerFactory();
ICronTrigger trigger = new CronTriggerImpl("UploadTrigger");
trigger.CronExpressionString = time;
IJobDetail jobDetail = new JobDetailImpl("UploadJob", typeof(UploadJob));
IScheduler schedule = scheduleFactory.GetScheduler();
schedule.ScheduleJob(jobDetail, trigger);
schedule.Start();
//测试是不是多线程
for (int i = 0; i < 10; i++)
{
Thread.Sleep(10000); //让线程休息10秒钟
Console.WriteLine("--------------------");
}
}
}
public class UploadJob : IJob
{
public void Execute(IJobExecutionContext context)
{
//这里放置你的计划任务代码
Console.WriteLine("Fired");
}
}
}
运行后的效果: