Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.12 KB | None | 0 0
  1. using Quartz;
  2. using Quartz.Impl;
  3. using System;
  4. using System.Collections.Specialized;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7.  
  8. /**
  9.  *  https://www.quartz-scheduler.net/
  10.  */
  11. namespace SampleQuartznet
  12. {
  13.     public partial class Main : Form
  14.     {
  15.         public Main()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void Main_Load(object sender, EventArgs e)
  21.         {
  22.             ScheduleMessageAsync();
  23.         }
  24.  
  25.         private static async Task ScheduleMessageAsync()
  26.         {
  27.             // construct a scheduler factory
  28.             NameValueCollection props = new NameValueCollection
  29.             {
  30.                 { "quartz.serializer.type", "binary" }
  31.             };
  32.             StdSchedulerFactory factory = new StdSchedulerFactory(props);
  33.  
  34.             // get a scheduler
  35.             IScheduler sched = await factory.GetScheduler();
  36.             await sched.Start();
  37.  
  38.             // define the job and tie it to our HelloJob class
  39.             IJobDetail job = JobBuilder.Create<HelloJob>()
  40.                 .WithIdentity("myJob", "group1")
  41.                 .Build();
  42.  
  43.             // Trigger the job to run now, and then every 40 seconds
  44.             ITrigger trigger = TriggerBuilder.Create()
  45.                 .WithIdentity("myTrigger", "group1")
  46.                 .StartNow()
  47.                 //.StartAt(DateBuilder.DateOf(0, 0, 0, 3, 12, 2020))
  48.                 //.EndAt(DateBuilder.DateOf(0, 0, 0, 3, 3, 2021))
  49.                 //.WithCronSchedule("0 0 0 1/5 * ? *")
  50.                 .WithSimpleSchedule(x => x
  51.                     .WithIntervalInSeconds(40)
  52.                     .RepeatForever())
  53.                 //.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(9, 30)) // execute job daily at 9:30
  54.                 //.WithPriority(1)
  55.             .Build();
  56.  
  57.             await sched.ScheduleJob(job, trigger);
  58.         }
  59.  
  60.         public class HelloJob : IJob
  61.         {
  62.             public async Task Execute(IJobExecutionContext context)
  63.             {
  64.                 await Console.Out.WriteLineAsync("HelloJob is executing...");
  65.             }
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement