using Quartz;
using Quartz.Impl;
using Quartz.Spi;
using System;
using System.Configuration;
using System.IO;
namespace ITMSService
{publicstaticclassSchedulerTools{static IScheduler sched =null;publicstaticvoidStartJob(){
File.AppendAllText("C:///Log/log.txt", DateTime.Now.ToString()+"服务启动...\r\n");//日志文件
sched =newStdSchedulerFactory().GetScheduler();//实例化调度器
JobDetailImpl StockReport =newJobDetailImpl("jobStockReport",typeof(StockJob));//需要执行的任务StockJob--这里面指的是下面使用到的类
IMutableTrigger triggerStockReport = CronScheduleBuilder.DailyAtHourAndMinute(1,30).Build();// 每天 1:30 执行一次任务
triggerStockReport.Key =newTriggerKey("stockElimina");//触发器
sched.ScheduleJob(StockReport, triggerStockReport);//调度器执行调度的定时任务
sched.Start();//开始调度}publicstaticvoidStopJob(){
File.AppendAllText("E://ITMS/ITMSHis/log.txt", DateTime.Now.ToString()+"服务停止...\r\n");if(sched !=null){
sched.Shutdown();}}}}
需要执行的任务
using EntityInfo;
using Quartz;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace ITMSService
{publicclassStockJob: IJob
{privatestatic ConnectionStringMySQLDB db =newConnectionStringMySQLDB();publicvoidExecute(IJobExecutionContext context){
File.AppendAllText("E://ITMS/ITMSHis/log.txt", DateTime.Now.ToString()+"统计库存任务启动...\r\n");try{
db.CommandTimeout =12000000;
db.Execute("call StockNum()");//调用存储过程}catch(Exception ex){
File.AppendAllText("E://ITMS/ITMSHis/log.txt", DateTime.Now.ToString()+ ex.ToString()+"\r\n");}
File.AppendAllText("E://ITMS/ITMSHis/log.txt", DateTime.Now.ToString()+"统计库存任务完成...\r\n");}}}
Windows Services调用定时任务
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
namespace ITMSService
{public partial classITMSXK: ServiceBase
{publicITMSXK(){InitializeComponent();}protected override voidOnStart(string[] args){
SchedulerTools.StartJob();//Windows Services启动时调用定时任务}protected override voidOnStop(){
SchedulerTools.StopJob();}}}