In the actual project development process, there will be such functional requirements: the need to create some job timed trigger run, such as the synchronization of some data.
So how do you implement this timer job in the. Net Framework?
Here is the use of third-party components quartz.net to achieve (source location: https://github.com/quartznet/quartznet)
For more information, see the following steps:
1): First create a console application in VS and then download the Quartz.net component from NuGet and reference it to the current project. We have downloaded version 3.0, note: This version differs from the previous version 2.0.
2): Inherit IJob interface, implement Excute method
Public classEricsimplejob:ijob { PublicTask Execute (Ijobexecutioncontext context) {Console.WriteLine ("Hello Eric, Job executed."); returnTask.completedtask; } } Public classEricanothersimplejob: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 the configuration between IScheduler, Ijobdetails and Itrigger
Static AsyncTask testasyncjob () {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 (); Ijobdetail Anotherjob= jobbuilder.create<ericanothersimplejob>() . Withidentity ("Ericanotherjob","Ericgroup") . Build (); Itrigger Trigger=triggerbuilder.create (). Withidentity ("Erictrigger","Ericgroup") . Withsimpleschedule (x= X.withintervalinseconds (5). RepeatForever ()). Build (); Itrigger Anothertrigger=triggerbuilder.create (). Withidentity ("Ericanothertrigger","Ericgroup") . Withsimpleschedule (x= X.withintervalinseconds (5). RepeatForever ()). Build (); awaitSched. Schedulejob (Job, trigger); awaitSched. Schedulejob (Anotherjob, Anothertrigger); }
4): Completion of the call in the main method, because it is asynchronous processing, so here with Console.readkey () to complete the main thread block
Static void Main (string[] args) { testasyncjob (); Console.readkey (); }
5): The final result is, two jobs to keep the screen and file output string
For more information, please refer to the following links:
Https://www.cnblogs.com/MingQiu/p/8568143.html
6): If we want to register this as a Windows service and automatically process the corresponding job after the corresponding Service starts, please refer to the following link:
Http://www.cnblogs.com/mingmingruyuedlut/p/9033159.html
For a 2.0 version of Quartz.net please refer to the following link:
Https://www.quartz-scheduler.net/download.html
Https://www.codeproject.com/Articles/860893/Scheduling-With-Quartz-Net
Https://stackoverflow.com/questions/8821535/simple-working-example-of-quartz-net
C # implements schedule job processing through Quartz. NET