1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| using Quartz; using Quartz.Impl; using Quartz.Logging; using System; using System.IO; using System.Threading.Tasks; namespace QuartzDotNetDemo { class Program { static async Task Main(string[] args) { LogProvider.SetCurrentLogProvider(new ConsoleLogProvider()); StdSchedulerFactory factory = new StdSchedulerFactory(); IScheduler scheduler = await factory.GetScheduler(); await scheduler.Start(); IJobDetail job = JobBuilder.Create<HelloJob>().WithIdentity("job1", "group1").Build(); ITrigger trigger = TriggerBuilder.Create().WithIdentity("trigger1", "group1").StartNow().WithSimpleSchedule(x => x.WithIntervalInSeconds(10) .RepeatForever()).Build(); await scheduler.ScheduleJob(job, trigger); await Task.Delay(TimeSpan.FromSeconds(60)); await scheduler.Shutdown(); Console.WriteLine("Press any key to close the application"); Console.ReadKey(); } } public class ConsoleLogProvider: ILogProvider { public Logger GetLogger(string name) { return(level, func, exception, parameters) => { if(level >= LogLevel.Info && func != null) { Console.WriteLine("[" + DateTime.Now.ToLongTimeString() + "] [" + level + "] " + func(), parameters); } return true; }; } public IDisposable OpenMappedContext(string key, object value, bool destructure = false) { throw new NotImplementedException(); } public IDisposable OpenNestedContext(string message) { throw new NotImplementedException(); } } public class HelloJob: IJob { public async Task Execute(IJobExecutionContext context) { await Console.Out.WriteLineAsync("Greetings from HelloJob!"); } } }
|