码迷,mamicode.com
首页 > Windows程序 > 详细

c# quartz定时任务框架的使用

时间:2018-10-15 14:20:18      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:arp   script   roc   instr   windows   inter   overwrite   net   top   

一、新建windows服务项目,并从nuget引用下图标记的类库:

技术分享图片

二、鼠标右键单击Service1.cs,点击查看代码,把类文件修改成如下:

Service1.cs

    public partial class Service1 : ServiceBase
    {
        private IScheduler scheduler;

        public Service1()
        {
            InitializeComponent();

            #region Quartz
            try
            {
                //调度器
                ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
                scheduler = schedulerFactory.GetScheduler();
                //任务、触发器执行配置
                XMLSchedulingDataProcessor processor = new XMLSchedulingDataProcessor(new SimpleTypeLoadHelper());
                Stream s = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + @"\Config\quartz_jobs.xml").BaseStream;
                processor.ProcessStream(s, null);
                processor.ScheduleJobs(scheduler);
            }
            catch (Exception ex)
            {
                //log 启动Quartz失败
            }
            #endregion
        }

        protected override void OnStart(string[] args)
        {
            scheduler.Start();
            //log 服务启动
        }

        protected override void OnStop()
        {
            if (scheduler != null)
            {
                scheduler.Shutdown(false);
            }
            //log 服务结束
        }
    }

三、在windows服务项目下建一个Config文件夹,并添加quartz_jobs.xml配置文件如下:

quartz_jobs.xml

<?xml version="1.0" encoding="utf-8" ?>
<!-- This file contains job definitions in schema version 2.0 format -->

<job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">
  <processing-directives>
    <overwrite-existing-data>true</overwrite-existing-data>
  </processing-directives>
  <schedule>
    <!--TestJob Start-->
    <job>
      <name>TestJob</name>
      <group>JobGroup</group>
      <description>测试定时任务</description>
      <job-type>WindowsService1.Job.TestJob,WindowsService1</job-type>
      <durable>true</durable>
      <recover>false</recover>
    </job>
    <trigger>
      <!--<simple>
        <name>TestTrigger</name>
        <group>TriggerGroup</group>
        <job-name>TestJob</job-name>
        <job-group>JobGroup</job-group>
        <misfire-instruction>SmartPolicy</misfire-instruction>
        <repeat-count>-1</repeat-count>
        <repeat-interval>1000</repeat-interval>
      </simple>-->
      <cron>
        <name>TestTrigger</name>
        <group>TriggerGroup</group>
        <job-name>TestJob</job-name>
        <job-group>JobGroup</job-group>
        <start-time>2018-10-12T06:00:00+08:00</start-time>
        <cron-expression>0 0/1 * * * ?</cron-expression>
      </cron>
    </trigger>
    <!--TestJob End-->    
  </schedule>
</job-scheduling-data>

四、在windows服务项目下建一个Job文件夹,并添加TestJob.cs文件如下:

TestJob.cs

    [PersistJobDataAfterExecution]//保存执行状态
    [DisallowConcurrentExecution]//不允许并发执行
    public class TestJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            //to do...
        }
    }

五、在windows服务项目下建一个Bat文件夹,并添加如下批处理文件:

install.bat

sc create 服务名称 binPath= "E:\WindowsService1.exe" start= auto
sc description 服务名称 "服务描述"
pause

start.bat

net start 服务名称
pause

stop.bat

net stop 服务名称
pause 

uninstall.bat

sc delete 服务名称
pause 

  

c# quartz定时任务框架的使用

标签:arp   script   roc   instr   windows   inter   overwrite   net   top   

原文地址:https://www.cnblogs.com/by-lhc/p/9790267.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!