Microsoft Windows Service (3) uses Quartz.net to schedule tasks,

Source: Internet
Author: User

Microsoft Windows Service (3) uses Quartz.net to schedule tasks,

Quartz is another open-source project of OpenSymphony open-source organization in the Job scheduling field. Quartz.net is the transplanted version of Quartz. Quartz can be used to create complex programs such as simple or running ten, hundreds, or even tens of thousands of Jobs.

Official website tutorial: http://www.quartz-scheduler.net/documentation/index.html

  

Advantages:

1. Full, start time, interval, times, and end time can be specified.

2. Convenient. There is no need to consider various thread issues.

 

Download:

Install-Package Quartz

  

  

 

Development:

1. job 2. trigger 3. scheduler

 

 

Job: a task item that defines the tasks to be executed.

There is only one method to implement the IJob interface.

  

class SampleJob : IJob    {        public void Execute(IJobExecutionContext context)        {            File.AppendAllText("d:\\1.txt", "good");        }    }

 

Trigger: the trigger that defines when to execute multiple times..

Constructor: public SimpleTriggerImpl (string name, string group, DateTimeOffset startTimeUtc, DateTimeOffset? EndTimeUtc, int repeatCount, TimeSpan repeatInterval );

  

You can use the above ctor to create a new call trigger.

 

 

Sched: scheduler, which schedules the combination of triggers and tasks.

  

var factory = new StdSchedulerFactory();var sched = factory.GetScheduler();

 

Run:

A. Wrap the IJob into IJobDetail and use public JobDetailImpl (string name, Type jobType );

B. Scheduling Method for calling the ischedoffset interface: DateTimeOffset ScheduleJob (IJobDetail jobDetail, ITrigger trigger );

C. Call the ischedayinterface to Start the method: void Start (); or void StartDelayed (TimeSpan delay );

  

 

Stop:

Call the ischeddown interface start method: void Shutdown ();

 

 

Download Code:Click to download

 

 

Use Quartz and TopShelf to create a service:

1 class Program 2 {3 static void Main (string [] args) 4 {5 HostFactory. run (x => 6 {7 // the service to be configured 8 x. service <RuntimeService> (c => 9 {10 c. constructUsing (name => new RuntimeService (); 11 c. whenStarted (s => s. begin (); 12 c. whenStopped (s => s. stop (); 13}); 14 // Service Running identity 15 x. runAsLocalSystem (); 16 17 x. setDescription ("service description"); 18 x. setDisplayName ("display name"); 19 x. setServiceName ("service name"); 20}); 21} 22} 23 24 class RealJob: IJob25 {26 public void Execute (IJobExecutionContext context) 27 {28 Console. writeLine (DateTime. now. toLongTimeString (); 29} 30} 31 32 class RuntimeService33 {34 private isched1_sched; 35 public RuntimeService () 36 {37 var factory = new StdSchedulerFactory (); 38 sched = factory. getScheduler (); 39} 40 public void Begin () 41 {42 sched. scheduleJob (new JobDetailImpl ("job", typeof (RealJob), 43 new SimpleTriggerImpl ("trig", "group",-1, new TimeSpan (0, 0, 1); 44 sched. start (); 45} 46 47 public void Stop () 48 {49 sched. shutdown (); 50} 51}

 

Related Article

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.