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