C # implements the timer job through Quartz. NET and registers it as a Windows Service

Source: Internet
Author: User

A previous article describes how to implement Timer Job through Quartz. NET (http://www.cnblogs.com/mingmingruyuedlut/p/8037263.html)

On this basis, how to register the implemented timer job as a Windows Service, see the following steps:

1): Create a project for Windows service in VS

2): Inherit the IJob interface to implement the write value to the text file

usingQuartz;usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacequartztimerwinserapp{ Public classEricsimplejob:ijob { PublicTask Execute (Ijobexecutioncontext context) {stringfilepath =@"C:\timertest.txt"; if(!file.exists (filepath)) {                using(FileStream fs =file.create (filepath)) { }            }            using(StreamWriter SW =NewStreamWriter (filepath,true) ) {SW.            WriteLine (DateTime.Now.ToLongTimeString ()); }            returnTask.completedtask; }    }}

3): Complete IScheduler, Ijobdetail and Itrigger related configurations

usingQuartz;usingQuartz.impl;usingSystem;usingSystem.Collections.Generic;usingSystem.Collections.Specialized;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacequartztimerwinserapp{ Public classJobscheduler { Public Async voidStart () {varProps =NewNameValueCollection {{"Quartz.serializer.type","binary" }            }; Stdschedulerfactory schedfact=Newstdschedulerfactory (props); IScheduler sched=awaitSchedfact.getscheduler (); awaitSched.            Start (); Ijobdetail Job= jobbuilder.create<ericsimplejob>()                . Withidentity ("Ericjob","Ericgroup")                .            Build (); Itrigger Trigger=triggerbuilder.create (). Withidentity ("Erictrigger","Ericgroup")                . Withsimpleschedule (x= X.withintervalinseconds (5). RepeatForever ()).            Build (); awaitSched.        Schedulejob (Job, trigger); }         Public Async voidStop () {IScheduler sched=awaitStdschedulerfactory.getdefaultscheduler (); awaitSched.        Shutdown (); }    }}

4): Complete the process of Service1 in the project

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Diagnostics;usingSystem.Linq;usingsystem.serviceprocess;usingSystem.Text;usingSystem.Threading.Tasks;namespacequartztimerwinserapp{ Public Partial classservice1:servicebase {Jobscheduler Scheduler;  PublicService1 () {InitializeComponent (); }        protected Override voidOnStart (string[] args) {Scheduler=NewJobscheduler (); Scheduler.        Start (); }        protected Override voidOnStop () {if(Scheduler! =NULL) Scheduler.        Stop (); }    }}

5): Complete the processing of main function in Program.cs

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.serviceprocess;usingSystem.Text;usingSystem.Threading.Tasks;namespacequartztimerwinserapp{Static classProgram {/// <summary>        ///The main entry point for the application. /// </summary>        Static voidMain () {servicebase[] servicestorun; ServicesToRun=Newservicebase[] {NewService1 ()};        Servicebase.run (ServicesToRun); }    }}

6): After completing the above code, the build EXE executes the file, puts it into the specified directory, and then uses the InstallUtil.exe in the. Net Framework to complete the registration of the service (for example, InstallUtil.exe as directory: \ C windows\microsoft.net\framework64\v4.0.30319), note: To run cmd as an administrator

If you want to uninstall the corresponding service, then the corresponding command behavior: InstallUtil.exe/ u C:\CustomerWinService\....exe

7): To the service management interface to find the service just installed, and then right-click to start, then you can go to the corresponding TXT file to see what the Timer Job writes

For more information, please refer to the following links:

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/

C # implements the timer job through Quartz. NET and registers it as a Windows Service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.