Service1.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.ServiceProcess;
  8. using System.Text;
  9. namespace StartService
  10. {
  11. public partial class Service1 : ServiceBase
  12. {
  13. private System.Timers.Timer timer1 = new System.Timers.Timer();
  14. public Service1()
  15. {
  16. timer1.Interval = 1800000;
  17. timer1.Elapsed += Timer1_Elapsed;
  18. InitializeComponent();
  19. }
  20. private void Timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  21. {
  22. timer1.Stop();
  23. try
  24. {
  25. ServiceController serviceController = new ServiceController("EMIS.Service");
  26. if (serviceController.Status != ServiceControllerStatus.Running && serviceController.Status != ServiceControllerStatus.StartPending && serviceController.Status != ServiceControllerStatus.ContinuePending)
  27. {
  28. serviceController.Start();
  29. }
  30. }
  31. catch (System.Exception err)
  32. {
  33. }
  34. timer1.Start();
  35. }
  36. protected override void OnStart(string[] args)
  37. {
  38. timer1.Start();
  39. }
  40. protected override void OnStop()
  41. {
  42. timer1.Stop();
  43. }
  44. }
  45. }