分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

quartz.net 入门

发布时间:2023-09-06 01:20责任编辑:顾先生关键词:暂无标签

1,新建个控制台项目并引入包quartz.net。

2,新建作业类 HelloJob继承自IJob

  

 public class HelloJob : IJob ???{ ???????public void Execute(IJobExecutionContext context) ???????{ ???????????Console.WriteLine("你好"); ???????} ???}

3,Main方法里写入以下代码:

 static void Main(string[] args) ???????{ ???????????try ???????????{ ???????????????Common.Logging.LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter { Level = Common.Logging.LogLevel.Info }; ???????????????IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler(); ???????????????// and start it off ???????????????scheduler.Start(); ???????????????// define the job and tie it to our HelloJob class ???????????????IJobDetail job = JobBuilder.Create<HelloJob>() ???????????????????.WithIdentity("job1", "group1") ???????????????????.Build(); ???????????????????????????????//秒 分 时 月 ?2点到13点之间每秒执行一次方法 ???????????????ITrigger trigger = ?????????????TriggerBuilder.Create().WithIdentity("trigger1", "group1") ???????????????.WithCronSchedule("0/1 * 2-12 * * ?").Build(); ???????????????scheduler.ScheduleJob(job, trigger); ???????????????// some sleep to show what‘s happening ???????????????Thread.Sleep(TimeSpan.FromSeconds(60)); ???????????????//// and last shut down the scheduler when you are ready to close your program ???????????????//scheduler.Shutdown(); ???????????} ???????????catch (SchedulerException se) ???????????{ ???????????????Console.WriteLine(se); ???????????} ???????????Console.WriteLine("Press any key to close the application"); ???????????Console.ReadKey(); ???????}

  

这里我们用WithCronSchedule方法来配置定时任务的时间,"0/1 * 2-12 * * ?" ?代表每天2点到13点,每秒执行一次方法。

quartz.net 入门

原文地址:http://www.cnblogs.com/XM-CHC/p/7736524.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved