首页 > Web开发 > 详细

Quartz.NET 作业调度使用

时间:2018-07-16 17:43:54      阅读:1032      评论:0      收藏:0      [点我收藏+]

Quartz.NET的使用方法有很多,今天使用Quartz.NET3.0.6的时候发现和2.0版本的语法不太一样,百度上找了一圈也没有找到解决办法

后来在GitHub上下载源代码解决了

技术分享图片

实现每隔10s将时间写入txt文件

第一步、新建一个类,实现IJob接口的Execute方法Task Execute(IJobExecutionContext context)

    public class TimeJob : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            string filePath = @"D:\\log.txt";
            if (!File.Exists(filePath))
                File.Create(filePath);
            File.AppendAllText(filePath, DateTime.Now + Environment.NewLine);
            Console.WriteLine(DateTime.Now + Environment.NewLine);
            return Task.FromResult(true);
        }

        
    }

 第二步、实现任务调度

 static void Main(string[] args)
        {
            TestJob();
            Console.ReadKey();
        }


        static async Task TestJob()
        {
            //创建调度器工厂
            ISchedulerFactory factory = new StdSchedulerFactory();
            //创建调度器
            IScheduler scheduler = await factory.GetScheduler();
            
            //创建一个任务
            IJobDetail job = JobBuilder.Create<TimeJob>().WithIdentity("myJob1", "group1").Build();
            //创建一个触发器
            ITrigger trigger = TriggerBuilder.Create().WithIdentity("myTrigger1", "group1").StartNow().
                WithSimpleSchedule(a => a.WithIntervalInSeconds(10).RepeatForever()).Build();
            //将任务和触发器添加到调度器里
            await scheduler.ScheduleJob(job, trigger);
            //开始执行
            await scheduler.Start();
        }

 参考 https://github.com/quartznet/quartznet

 

Quartz.NET 作业调度使用

原文:https://www.cnblogs.com/ZJ199012/p/9318814.html

(0)
(5)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!