C# -- Quartz.Net入门案例

1. 入门案例

using Quartz;
using Quartz.Impl;

 public class PrintTime : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            return Task.Factory.StartNew(new Action(() => Console.WriteLine($"当前时间: {DateTime.Now.ToString()}")));
        }
    }
View Code
        static void Main(string[] args)
        {
            startShowTime();
            Console.ReadKey();
        }

        static async void startShowTime()
        {
            ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
            IScheduler scheduler = await schedulerFactory.GetScheduler();

            IJobDetail job = JobBuilder.Create<PrintTime>().Build();


            try
            {
                ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create().WithSimpleSchedule(x => x.WithIntervalInSeconds(3).WithRepeatCount(50)).Build();

                await scheduler.ScheduleJob(job, trigger);

                await scheduler.Start();

            }
            catch (Exception EX)
            {
                Console.WriteLine(EX.Message);
                await scheduler.Shutdown();
            }

        }

 

2. 运行结果

posted on 2019-05-30 11:51  在代码的世界里游走  阅读(833)  评论(0编辑  收藏  举报