Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Quartz;
- using Quartz.Impl;
- using System;
- using System.Collections.Specialized;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- /**
- * https://www.quartz-scheduler.net/
- */
- namespace SampleQuartznet
- {
- public partial class Main : Form
- {
- public Main()
- {
- InitializeComponent();
- }
- private void Main_Load(object sender, EventArgs e)
- {
- ScheduleMessageAsync();
- }
- private static async Task ScheduleMessageAsync()
- {
- // construct a scheduler factory
- NameValueCollection props = new NameValueCollection
- {
- { "quartz.serializer.type", "binary" }
- };
- StdSchedulerFactory factory = new StdSchedulerFactory(props);
- // get a scheduler
- IScheduler sched = await factory.GetScheduler();
- await sched.Start();
- // define the job and tie it to our HelloJob class
- IJobDetail job = JobBuilder.Create<HelloJob>()
- .WithIdentity("myJob", "group1")
- .Build();
- // Trigger the job to run now, and then every 40 seconds
- ITrigger trigger = TriggerBuilder.Create()
- .WithIdentity("myTrigger", "group1")
- .StartNow()
- //.StartAt(DateBuilder.DateOf(0, 0, 0, 3, 12, 2020))
- //.EndAt(DateBuilder.DateOf(0, 0, 0, 3, 3, 2021))
- //.WithCronSchedule("0 0 0 1/5 * ? *")
- .WithSimpleSchedule(x => x
- .WithIntervalInSeconds(40)
- .RepeatForever())
- //.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(9, 30)) // execute job daily at 9:30
- //.WithPriority(1)
- .Build();
- await sched.ScheduleJob(job, trigger);
- }
- public class HelloJob : IJob
- {
- public async Task Execute(IJobExecutionContext context)
- {
- await Console.Out.WriteLineAsync("HelloJob is executing...");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement