C # uses Quartz. NET to implement Schedule job processing

Source: Internet
Author: User

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

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.