Use Quartz. net in. Net, and use quartz.net in. net.

Source: Internet
Author: User

Use Quartz. net in. Net, and use quartz.net in. net.

Quartz.net is a job scheduling framework.

1. Add quartz.net references to the project (nuget management is used here)

 

 

 

Create a new class TimingJob, which is mainly used to implement the task Logic

Using Quartz; using System; namespace QuartzNetDemo {// <summary> // scheduled task class /// </summary> public class TimingJob: IJob {public void Execute (IJobExecutionContext context) {// write the logic code to be periodically executed on the Console. writeLine ("task executed ");}}}

 

In Program. cs: (here is the console application)

using Quartz;using Quartz.Impl;using System;namespace QuartzNetDemo{    class Program    {        static void Main(string[] args)        {            ISchedulerFactory sf = new StdSchedulerFactory();            IScheduler scheduler = sf.GetScheduler();            IJobDetail job = JobBuilder.Create<TimingJob>().WithIdentity("job1", "mygroup").Build();            ITrigger trigger = TriggerBuilder.Create().StartAt(DateTime.Now.AddSeconds(5)).WithCronSchedule("/2 * * ? * *").Build();            scheduler.ScheduleJob(job,trigger);            scheduler.Start();        }    }}

When the program is running, information is output every 2 seconds after 5 seconds

 

 

Cron expression:

The cron expression in quartz. NET is like "/5 **? * ** "This is a 7-bit expression. The last year is optional. The expression is left to right, the second, minute, hour, day of the month, month, day of the week, and year.

Name Range Special characters that can be used
Seconds 0-59 ,-*/
Minutes 0-59 ,-*/
Hours 0-23 ,-*/
Day of month 1-31 ,-*? /L W
Month 1-12 or JAN-DEC ,-*/
Day of week 1-7 or SUN-SAT ,-*? /L #
Year Null, 1970-2099 ,-*/

 

Special characters and explanations:

, Or.
/A/B. A: Start Time and B frequency.
* Frequency. That is, every fluctuation.
-Interval.
? Any value. That is, every fluctuation. It can only be used in DayofMonth and DayofWeek. Specify one for another?
L indicates the end. It can only be used in DayofMonth and DayofWeek
W working days. Indicates the end. It can only be used in DayofWeek
# X # y. Only DayofMonth can be used. Week y of a month x

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.