以前的一篇文章講述瞭如何經過 Quartz .NET 實現 Timer Job (http://www.cnblogs.com/mingmingruyuedlut/p/8037263.html)html
在此基礎上如何將實現的Timer Job註冊成爲Windows Service,請看以下步驟:windows
1):在VS中建立Windows Service的工程app
2):繼承 IJob 接口實現對文本文件的寫值async
using Quartz; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace QuartzTimerWinSerApp { public class EricSimpleJob : IJob { public Task Execute(IJobExecutionContext context) { string filepath = @"C:\timertest.txt"; if (!File.Exists(filepath)) { using (FileStream fs = File.Create(filepath)) { } } using (StreamWriter sw = new StreamWriter(filepath, true)) { sw.WriteLine(DateTime.Now.ToLongTimeString()); } return Task.CompletedTask; } } }
3):完成 IScheduler, IJobDetail 和 ITrigger 的相關配置ide
using Quartz; using Quartz.Impl; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Text; using System.Threading.Tasks; namespace QuartzTimerWinSerApp { public class JobScheduler { public async void Start() { var props = new NameValueCollection { { "quartz.serializer.type", "binary" } }; StdSchedulerFactory schedFact = new StdSchedulerFactory(props); IScheduler sched = await schedFact.GetScheduler(); await sched.Start(); IJobDetail job = JobBuilder.Create<EricSimpleJob>() .WithIdentity("EricJob", "EricGroup") .Build(); ITrigger trigger = TriggerBuilder.Create() .WithIdentity("EricTrigger", "EricGroup") .WithSimpleSchedule(x => x.WithIntervalInSeconds(5).RepeatForever()) .Build(); await sched.ScheduleJob(job, trigger); } public async void Stop() { IScheduler sched = await StdSchedulerFactory.GetDefaultScheduler(); await sched.Shutdown(); } } }
4):完成工程中Service1的處理函數
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading.Tasks; namespace QuartzTimerWinSerApp { public partial class Service1 : ServiceBase { JobScheduler scheduler; public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { scheduler = new JobScheduler(); scheduler.Start(); } protected override void OnStop() { if (scheduler != null) scheduler.Stop(); } } }
5):完成Program.cs中Main函數的處理ui
using System; using System.Collections.Generic; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading.Tasks; namespace QuartzTimerWinSerApp { static class Program { /// <summary> /// The main entry point for the application. /// </summary> static void Main() { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new Service1() }; ServiceBase.Run(ServicesToRun); } } }
6):完成上述代碼以後,build出來的exe執行文件,放到指定的目錄中,而後用 .Net Framework 中的 InstallUtil.exe 完成對service的註冊 (例如InstallUtil.exe爲目錄爲:C:\Windows\Microsoft.NET\Framework64\v4.0.30319),命令行爲: InstallUtil.exe ‘your .exe file path’,注:要以管理員的身份運行cmdspa
若是想要卸載對應的服務,那麼對應的命令行爲:InstallUtil.exe /u C:\CustomerWinService\....exe命令行
7): 到Service管理界面找到剛剛安裝上的Service,而後右鍵啓動,以後就能夠到對應的txt文件中看到 Timer Job 所寫入的內容code
更多相關內容請參考以下連接:
http://www.codingdefined.com/2016/08/schedule-tasks-as-windows-service-using.html
http://www.c-sharpcorner.com/UploadFile/8f2b4a/how-to-installuninstall-net-windows-service-C-Sharp/