xuyaxiu 发表于 2017-6-29 09:23:02

windows 服务实现定时任务调度(Quartz.Net)

  我们通常在一些情况下需要软件具有一个自动执行某些任务的功能,但是又不希望直接启动软件,或者每次都要手动的来启动软件,这时我们可可以考虑到windows服务了。
  首先创建一个windows服务项目(详细信息请参阅:C#创建Windows Service(Windows 服务)基础教程)
  
  在创建好的项目中点击“单击此处切换到代码视图”切换到代码
  我们主要关注一下两个方法:
  · OnStart – 控制服务启动
  · OnStop – 控制服务停止
  例:





   public partial class Service1 : ServiceBase
   {
         public Service1()
         {
             InitializeComponent();
         }

         protected override void OnStart(string[] args)
         {
             //todo:这里是服务启动所执行的代码
         }

         protected override void OnStop()
         {
             //todo:这里是服务停止所执行的代码
         }
   }
View Code  下面我们可以写一个定时任务的功能了:



     private void StartDoSomething()
         {
             System.Timers.Timer timer = new System.Timers.Timer(10000); //间隔10秒
             timer.AutoReset = true;
             timer.Enabled = false;//执行一次
             timer.Elapsed += new ElapsedEventHandler(ExecutionCode);
             timer.Start();
         }

         private void ExecutionCode(object source, System.Timers.ElapsedEventArgs e)
         {
             string dtNow = DateTime.Now.ToString("HH:mm");
             if (dtNow == "12:00")
             {
               File.WriteAllText("D:/ExecutionService.txt", "服务执行了一次任务", Encoding.UTF8);
             }
         }
  然后在OnStart的方法中调用上面的StartDoSomething的方法



         protected override void OnStart(string[] args)
         {
             StartDoSomething();
         }   
  以上就可以算是一个简单的定时执行任务的windows服务了,这里我们还可以使用Quartz.Net来实现更加强大的任务调度功能。
  首先来介绍一下Quartz.Net这个框架:
  简介:Quartz.Net是一个开源的任务调度框架,非常强大,能够通过简单的配置帮助我们定时具体的操作。相对于我们用的线程里面while(true)然后sleep来执行某个操作,应该算的上是高端,大气,上档次了。目前最新版本是2.2,新的版本里面有些方法名发生了变化,从之前的版本用过来的人应该会有体会.这里我使用最常用,也是最稳定的方式--Windows服务里面使用Quartz.net,并且使用配置的方式来设置触发器。(以上内容摘自网络)
  简单的理解就是它能够帮我们定时的做事,相当于闹钟能够叫我们起床一样。
  目前最新的版本是Quartz.NET 2.2.3 大家可以在这里下载
  现在我们需要在刚刚创建的服务项目中引用如下文件:

  在配置文件中写好自己的配置(本例子演示定时访问指定网站)





<?xml version="1.0"?>
<configuration>
   <configSections>
   <sectionGroup name="JobList">
       <section name="Job" type="MyService1101.MyConfigHandler,MyService1101"/>
   </sectionGroup>
   </configSections>
   <startup>
   <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
   </startup>
   <JobList>
   <Job>   <!--这里是一个任务节点-->
       <add key="Url" value="http://www.baidu.com" /><!--需要访问的Url-->
       <add key="Hour" value="10" />   <!--开始时间小时,注意:这里的小时为0-23,如果是1点的话就是1,而不是01-->
       <add key="Minute" value="30"/>    <!--开始时间分钟,注意:同上0-59-->
   </Job>
   </JobList>
</configuration>
View Code  新建一个MyConfigHandler.cs类来读取自定义配置节点





public class MyConfigHandler : IConfigurationSectionHandler
   {
         public MyConfigHandler()
         {
         }

         public object Create(object parent, object configContext, System.Xml.XmlNode section)
         {
             NameValueCollection configs;
             NameValueSectionHandler baseHandler = new NameValueSectionHandler();
             configs = (NameValueCollection)baseHandler.Create(parent, configContext, section);
             return configs;
         }
   }
View Code  然后新建一个SystemScheduler类来创建调度程序





   public class SystemScheduler
   {
         private SystemScheduler()
         {
         }

         public static SystemScheduler CreateInstance()
         {
             return new SystemScheduler();
         }

         private IScheduler _scheduler;

         public void StartScheduler()
         {
             //这里读取配置文件中的任务开始时间
             int hour = int.Parse(((NameValueCollection)ConfigurationSettings.GetConfig("JobList/Job"))["Hour"]);
             int minute = int.Parse(((NameValueCollection)ConfigurationSettings.GetConfig("JobList/Job"))["Minute"]);

             ISchedulerFactory schedulerFactory = new StdSchedulerFactory();//内存调度
             _scheduler = schedulerFactory.GetScheduler();

             //创建一个Job来执行特定的任务
             IJobDetail synchronousData = new JobDetailImpl("SynchronousData", typeof(SynchronousData));
             //创建并定义触发器的规则(每天执行一次时间为:时:分)
             ITrigger trigger =
               TriggerBuilder.Create()
                     .WithDailyTimeIntervalSchedule(
                         a => a.WithIntervalInHours(24).OnEveryDay().StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(hour, minute))).Build();
             //将创建好的任务和触发规则加入到Quartz中
             _scheduler.ScheduleJob(synchronousData, trigger);
             //开始
             _scheduler.Start();
         }

         public void StopScheduler()
         {
             _scheduler.Shutdown();
         }
   }
View Code  新建一个SynchronousData类,让其实现IJob接口来实现SystemScheduler中自定义的任务





   public class SynchronousData : IJob
   {
         public void Execute(IJobExecutionContext context)
            {
             string Url = ((NameValueCollection)ConfigurationSettings.GetConfig("JobList/Job"))["Url"];
             WebClient wc = new WebClient();
             WebRequest wr = WebRequest.Create(new Uri(Url));
             using (StreamWriter sw = File.AppendText(@"d:\SchedulerService.txt"))
             {
               sw.WriteLine("------------------" + "MyService服务在:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "   执行了一次任务" + "------------------");
               sw.Flush();
             }
         }
   }
View Code  最后在OnStart中添加对这个调度程序的应用



         protected override void OnStart(string[] args)
         {
             SystemScheduler _systemScheduler = SystemScheduler.CreateInstance();
             _systemScheduler.StartScheduler();
         }   
  程序生成后我们可以通过指令安装它

  安装完成后在服务中会有一个新的服务项

  程序运行过后会在D:盘生成一个SchedulerService.txt文件

  本程序源码:下载
  2. Quartz 定时器时间设置
  http://www.360doc.com/content/11/1017/10/1542811_156808468.shtml

MichaelMC 发表于 2017-6-29 12:23:54

說明好詳細 感謝 !!
页: [1]
查看完整版本: windows 服务实现定时任务调度(Quartz.Net)