Quartz.Net simple task scheduling of tasks (1)

This link

https://github.com/sunshuaize/cnBlogDemos/tree/master/Quartz.Net%20%E4%BB%BB%E5%8A%A1%E8%B0%83%E5%BA%A6%E4%B9%8B%E7%AE%80%E5%8D%95%E4%BB%BB%E5%8A%A1(1)/DayWorkClose

The following code corresponds to the item to see

Three objects

1.IScheduler

2.IJobDetail

3.ITrigger

The first step, choose to create a task unit (the construction of the railroad tracks)

#region creating unit (time / carrier) 
the StdSchedulerFactory Factory = new new the StdSchedulerFactory (); 
IScheduler Scheduler = the await factory.GetScheduler ();
 the await scheduler.Start ();
 #endregion

Next, create a task (making train)

#region job
IJobDetail  jobDetail = JobBuilder.Create<HelloJob>()
                       .WithDescription("this is a job")
                       .WithIdentity("job1", "group1")
                        .Build();
#endregion

WithIdentity():

The first parameter (job1): a name to the current task (codename)

The second parameter (group1): grouping, because this task may be more, it can be a multi-group classification at

HelloJob.cs

using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DayWorkCloseService.DayWorkClose
{
    public class HelloJob : IJob
    {
        public async Task Execute(IJobExecutionContext context)
        {
            await Task.Run(() => {

                Console.WriteLine($@"{"张翼德"}"+DateTime.Now + "");

            });
        }
    }
}

 

The third step is to configure time strategy (to determine the departure time)

#region 时间策略
                ITrigger trigger = TriggerBuilder.Create()
                                   .WithIdentity("trigger1", "group1")
                                   .StartNow()
                                   .WithSimpleSchedule(x => x
                                     .WithIntervalInSeconds(1)
                                     .WithRepeatCount(10000))
                                   .Build();
                #endregion

WithIdentity (): ibid.

startNow (): Get started once

WithIntervalInSeconds (): once a second

WithRepeatCount (): maximum execution 10000

4. the task to the task and the time policy carrier unit (bolstering drink wine, ready to go)

await scheduler.ScheduleJob(jobDetail, trigger);

This is the complete code I created a new library, call not here

using DayWorkCloseService.DayWorkClose;
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DayWorkCloseService
{
    public class DayWorkCloseManager
    {

        public static async Task Init()
        {
            try
            {
                #region 创建单元 (时间轴/载体)
                StdSchedulerFactory factory = new StdSchedulerFactory();
                IScheduler scheduler = await factory.GetScheduler();
                await scheduler.Start();
                #endregion

                #region job
                IJobDetail  jobDetail = JobBuilder.Create<HelloJob>()
                    .WithDescription("this is a job")
                  .WithIdentity("job1", "group1")
                  .Build();
                #endregion

                #region 时间策略
                ITrigger trigger = TriggerBuilder.Create()
                                   .WithIdentity("trigger1", "group1")
                                   .StartNow()
                                   .WithSimpleSchedule(x => x
                                   .WithIntervalInSeconds(1)
                                   .WithRepeatCount(10000))
                                   .Build();
                #endregion

                // Tell quartz to schedule the job using our trigger
                await scheduler.ScheduleJob(jobDetail, trigger);

            }
            catch (SchedulerException se)
            {
                Console.WriteLine(se);
            }
        }
    }
}

Ready done, start calling now on the word, no, two

. DayWorkCloseManager.Init () GetAwaiter () GetResult ();. 

 Console.Read (); // this must be
 done

Guess you like

Origin www.cnblogs.com/mi21/p/12084703.html
Recommended