码迷,mamicode.com
首页 > Windows程序 > 详细

C# 使用 quartz.net 做定时任务

时间:2019-08-22 18:59:40      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:ops   在线   生成   execution   时间   sys   with   tor   tail   

Quartz.NET  是一套很好的任务调度框架。在设置定时时间的时候,使用了cron表达式很方便

简单代码

 public async Task beginStart()
        {
            //从工厂中获取一个调度器实例化
            IScheduler scheduler = await StdSchedulerFactory.GetDefaultScheduler();
            await scheduler.Start();       //开启调度器

            //==========例子1(简单使用)===========
            var type = Type.GetType("ConsoleTopshelf.HelloJob");

            IJobDetail job1 = JobBuilder.Create(type)   //创建一个作业
                .WithIdentity("作业名称", "作业组")
                .Build();

            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("触发器名称", "触发器组")
                .StartNow()
                .WithCronSchedule("/1 * * * * ? ")  //corn 表达式 每秒执行一次
                .Build();

            await scheduler.ScheduleJob(job1, trigger);      //把作业,触发器加入调度器。
        }

 

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

namespace ConsoleTopshelf
{
    /// <summary>
    /// 作业
    /// </summary>
    public class HelloJob : IJob
    {
        public async Task Execute(IJobExecutionContext context)
        {
            Console.WriteLine("HelloJob" + System.DateTime.Now);
        }
    }
}

 

corn表达式在线生成 http://cron.qqe2.com/

 

C# 使用 quartz.net 做定时任务

标签:ops   在线   生成   execution   时间   sys   with   tor   tail   

原文地址:https://www.cnblogs.com/su-king/p/11395937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!