Quartz.NET 作业调度使用

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

猜你喜欢

转载自www.cnblogs.com/ZJ199012/p/9318814.html