欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# -- Quartz.Net入门案例

程序员文章站 2022-06-28 22:49:06
1. 入门案例 using Quartz;using Quartz.Impl; public class PrintTime : IJob { public Task Execute(IJobExecutionContext context) { return Task.Factory.StartN ......

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()}")));
        }
    }
        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. 运行结果

C# -- Quartz.Net入门案例