Simple use of C # quartz.net timed tasks

Source: Internet
Author: User

Recently made a software that performs tasks on a timed basis.

When the task is executed, the log is logged with log4net and if there is an exception to the execution of the task, the message is sent to the specified object.

What I do is to perform a task at 9 and 16 points per day to record this:

First, get quartz.net,

You can actually use the vs2015 nuget Manager to download.

I made a quartzhelper, but in the main program, I still need other code.

Here is the code for Quartzhelper:

1 usingQuartz;2 usingQuartz.impl;3 4 namespacecong.utility5 {6      Public classQuartzhelper7     {8         /// <summary>9         ///time interval to perform tasksTen         /// </summary> One         /// <typeparam name= "T" >task class, must implement Ijob interface</typeparam> A         /// <param name= "seconds" >Time interval (units: milliseconds)</param> -          Public Static voidExecuteinterval<t> (intSecondswhereT:ijob -         { theIschedulerfactory factory =Newstdschedulerfactory (); -IScheduler Scheduler =Factory. Getscheduler (); -  -             //Ijobdetail job = jobbuilder.create<t> (). Withidentity ("Job1", "group1"). Build (); +Ijobdetail job = jobbuilder.create<t>(). Build (); -  +Itrigger trigger =triggerbuilder.create () A                 . Startnow () at. Withsimpleschedule (x =x.withintervalinseconds (seconds). RepeatForever ()) -                 . Build (); -  - Scheduler. Schedulejob (Job, trigger); -  - Scheduler. Start (); in         } -  to         /// <summary> +         ///specify time to perform tasks -         /// </summary> the         /// <typeparam name= "T" >task class, must implement Ijob interface</typeparam> *         /// <param name= "Cronexpression" >cron expression, which is an expression at a specified point in time</param> $          Public Static voidExecutebycron<t> (stringCronexpression)whereT:ijobPanax Notoginseng         { -Ischedulerfactory factory =Newstdschedulerfactory (); theIScheduler Scheduler =Factory. Getscheduler (); +  AIjobdetail job = jobbuilder.create<t>(). Build (); the  +             //DateTimeOffset startTime = Datebuilder.nextgivenseconddate (DateTime.Now.AddSeconds (1), 2); -             //DateTimeOffset endTime = Datebuilder.nextgivenseconddate (DateTime.Now.AddYears (2), 3); $  $Icrontrigger trigger =(Icrontrigger) triggerbuilder.create () -                 //. StartAt (StartTime). EndAt (endTime) -                 . Withcronschedule (cronexpression) the                 . Build (); - Wuyi Scheduler. Schedulejob (Job, trigger); the  - Scheduler. Start (); Wu  -             //Thread.Sleep (Timespan.fromdays (2)); About             //Scheduler. Shutdown (); $         } -     } -  -     #regionTask Execution Example A     //Public class Myjob:ijob +     //{ the     //Public void Execute (Ijobexecutioncontext context) -     //    { $     //Console.WriteLine ("Executed ..." + DateTime.Now.ToString ("Yyyy/mm/dd HH:mm:ss")); the     //    } the     //}  the     #endregion the}

The approximate process is this:
Instantiate a plan factory object through Ischedulerfactory, and use a factory to instantiate a scheduler plan object,

Then instantiate a Task object job, which is the object that defines the task content, and needs to pass in a class that implements the Ijob interface, where I use generics,

Next, you need an object that can trigger the task, there are two kinds of

One is the Itrigger, which is used for specification time interval, after the plan starts, will trigger the execution task according to the interval time value;

The other is Icrontrigger, which is the time that is executed through the CRON expression specification, which triggers the execution of a task after the scheduled start, if the time meets the specified time.

Finally, the job and trigger are passed into the Plan scheduler, the plan begins.

The bottom-most example of a task is an example,

The Ijob interface and the Execute method must be implemented.

If you need to invoke data outside of the class within the Execute method, I am calling the myjob by defining a method of the static class in the project.

Code for program start plan:

string cronexpression = "0 0 9,16 * *?"; = = This refers to 9 points and 16 points per day to perform tasks
quartzhelper.executebycron<myjob> (cronexpression); = = This is called cron scheduling method

Simply say the cron expression,

"0 0 9,16 * *? ",

Order from left to right

0: Seconds

0: minutes

9,16: Hours, comma-delimited

If you need more detailed instructions, you can Baidu.

Simple use of C # quartz.net timed tasks

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.