ServiceRunner.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using Quartz;
  2. using Quartz.Impl;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Topshelf;
  9. namespace SMP.Tool.TimingTask.Core
  10. {
  11. public class ServiceRunner : ServiceControl, ServiceSuspend
  12. {
  13. private readonly IScheduler scheduler;
  14. public ServiceRunner()
  15. {
  16. scheduler = StdSchedulerFactory.GetDefaultScheduler().Result;
  17. }
  18. public bool Start(HostControl hostControl)
  19. {
  20. scheduler.Start();
  21. //具体任务启动可在配置文件编写
  22. for (var i = 0; i < JobConfig.jobs.Length; i++)
  23. {
  24. IJobDetail job1 = JobConfig.jobs[i];
  25. ITrigger trigger1 = TriggerBuilder.Create().StartNow().WithCronSchedule(JobConfig.crons[i]).Build();
  26. scheduler.ScheduleJob(job1, trigger1);
  27. }
  28. return true;
  29. }
  30. public bool Continue(HostControl hostControl)
  31. {
  32. scheduler.ResumeAll();
  33. return true;
  34. }
  35. public bool Pause(HostControl hostControl)
  36. {
  37. scheduler.PauseAll();
  38. return true;
  39. }
  40. public bool Stop(HostControl hostControl)
  41. {
  42. scheduler.Shutdown(false);
  43. return true;
  44. }
  45. }
  46. }