使用Windows Services执行定时任务调用存储过程

Quartz应用

using Quartz;
using Quartz.Impl;
using Quartz.Spi;
using System;
using System.Configuration;
using System.IO;

namespace ITMSService
{
    public static class SchedulerTools
    {
        static IScheduler sched = null;
        public static void StartJob()
        {
            File.AppendAllText("C:///Log/log.txt", DateTime.Now.ToString() + "服务启动...\r\n");//日志文件

            sched = new StdSchedulerFactory().GetScheduler();//实例化调度器
           
            JobDetailImpl StockReport = new JobDetailImpl("jobStockReport", typeof(StockJob));//需要执行的任务StockJob--这里面指的是下面使用到的类
            IMutableTrigger triggerStockReport = CronScheduleBuilder.DailyAtHourAndMinute(1, 30).Build();    // 每天 1:30  执行一次任务
            triggerStockReport.Key = new TriggerKey("stockElimina");//触发器
            sched.ScheduleJob(StockReport, triggerStockReport); //调度器执行调度的定时任务  
            sched.Start();//开始调度
        }
        public static void StopJob()
        {
            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
{
    public class StockJob : IJob
    {
        private static ConnectionStringMySQLDB db = new ConnectionStringMySQLDB();   
        public void Execute(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 class ITMSXK : ServiceBase
    {
        public ITMSXK()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            SchedulerTools.StartJob();//Windows Services启动时调用定时任务
        }

        protected override void OnStop()
        {
            SchedulerTools.StopJob();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值