.net FrameWork 使用Quartz定时器

引用 Quratz

创建类:使用作业

    /// 1.创建作业
    /// </summary>
    public class Myjob : IJob
    {
        public async Task Execute(IJobExecutionContext context)
        {
       //获取JobDtail的description var description = context.JobDetail.Description;
       //获取Jobdeteai var identity = context.JobDetail.Key.Name; System.IO.File.AppendAllText(@"d:\asd.txt", DateTime.Now.ToString()); } }

  

配置调度器

       //2.创建调度器
            ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
            var sch =await schedulerFactory.GetScheduler();

            //3.创建JobDetail,关联作业
            var job= JobBuilder.Create<Myjob>()
                .WithIdentity("myjob")
                .WithDescription("我的调度器")
                .Build();

            //4.创建触发器
           var trigger= TriggerBuilder.Create()
                .WithIdentity("myjob")
                .WithDescription("我的调度器")
                .WithSimpleSchedule(
                    x => x.WithIntervalInSeconds(3).RepeatForever()
                )
          .WithCronSchedule("cron表达式")//时间的cron .Build(); //5.关联触发器和job await sch.ScheduleJob(job, trigger); //6.启动调度器 await sch.Start();

  使用简单的调度器(对时间的进行操作)

.WithSimpleSchedule(
                    x => x.WithIntervalInSeconds(3).RepeatForever()
                )

  使用cron表达式(对时间的进行操作)

.WithCronSchedule("cron表达式")//时间的cron

  传值的时候还可以进行使用静态常量

posted @ 2022-05-15 21:28  电器小君子  阅读(293)  评论(0)    收藏  举报