//出现错误右键修复,自动添加包usingQuartz;usingQuartz.Impl;usingQuartz.Logging;usingSystem;usingSystem.Threading.Tasks;namespaceConsoleSkWork{classProgram{privatestaticasyncTaskMain(string[] args){LogProvider.SetCurrentLogProvider(newConsoleLogProvider());// Grab the Scheduler instance from the FactoryStdSchedulerFactory factory =newStdSchedulerFactory();IScheduler scheduler =await factory.GetScheduler();// and start it offawait scheduler.Start();// define the job and tie it to our HelloJob classIJobDetail job = JobBuilder.Create<HelloJob>().WithIdentity("job1","group1").Build();// Trigger the job to run now, and then repeat every 10 secondsITrigger trigger = TriggerBuilder.Create().WithIdentity("trigger1","group1").StartNow().WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever()).Build();// Tell Quartz to schedule the job using our triggerawait scheduler.ScheduleJob(job, trigger);// some sleep to show what's happeningawait Task.Delay(TimeSpan.FromSeconds(60));// and last shut down the scheduler when you are ready to close your programawait scheduler.Shutdown();Console.WriteLine("Press any key to close the application");Console.ReadKey();}// simple log provider to get something to the console//https://www.quartz-scheduler.net/documentation/quartz-3.x/quick-start.html#trying-out-the-applicationprivateclassConsoleLogProvider:ILogProvider{publicLoggerGetLogger(string name){return(level, func, exception, parameters)=>{if(level >= LogLevel.Info && func !=null){Console.WriteLine("["+ DateTime.Now.ToLongTimeString()+"] ["+ level +"] "+func(), parameters);}returntrue;};}publicIDisposableOpenNestedContext(string message){thrownewNotImplementedException();}publicIDisposableOpenMappedContext(string key,objectvalue,bool destructure =false){thrownewNotImplementedException();}}}publicclassHelloJob:IJob{publicasyncTaskExecute(IJobExecutionContext context){await Console.Out.WriteLineAsync("Greetings from HelloJob!");}}}
usingSystem;usingSystem.Threading.Tasks;usingQuartz;usingQuartz.Impl;usingQuartz.Logging;namespaceConsoleApp1{publicclassProgram{privatestaticasyncTaskMain(string[] args){LogProvider.SetCurrentLogProvider(newConsoleLogProvider());// Grab the Scheduler instance from the FactoryStdSchedulerFactory factory =newStdSchedulerFactory();IScheduler scheduler =await factory.GetScheduler();// and start it offawait scheduler.Start();// define the job and tie it to our HelloJob classIJobDetail job = JobBuilder.Create<HelloJob>().WithIdentity("job1","group1").Build();// Trigger the job to run now, and then repeat every 10 secondsITrigger trigger = TriggerBuilder.Create().WithIdentity("trigger1","group1").StartNow().WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever()).Build();// 将trigger监听器注册到调度器scheduler.ListenerManager.AddTriggerListener(newCustomTriggerListener());// Tell Quartz to schedule the job using our triggerawait scheduler.ScheduleJob(job, trigger);// some sleep to show what's happeningawait Task.Delay(TimeSpan.FromSeconds(60));// and last shut down the scheduler when you are ready to close your programawait scheduler.Shutdown();Console.WriteLine("Press any key to close the application");Console.ReadKey();}// simple log provider to get something to the consoleprivateclassConsoleLogProvider:ILogProvider{publicLoggerGetLogger(string name){return(level, func, exception, parameters)=>{if(level >= LogLevel.Info && func !=null){Console.WriteLine("["+ DateTime.Now.ToLongTimeString()+"] ["+ level +"] "+func(), parameters);}returntrue;};}publicIDisposableOpenNestedContext(string message){thrownewNotImplementedException();}publicIDisposableOpenMappedContext(string key,objectvalue,bool destructure =false){thrownewNotImplementedException();}}}publicclassHelloJob:IJob{publicasyncTaskExecute(IJobExecutionContext context){//获取当前时间DateTime currentDateTime = DateTime.UtcNow;await Console.Out.WriteLineAsync("当前日期和时间:"+ currentDateTime.AddHours(8));}}}