SonopostService.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.IO;
  3. using System.ServiceProcess;
  4. using System.Threading;
  5. using Vinno.FIS.Sonopost.Common;
  6. namespace Vinno.FIS.Sonopost.Service
  7. {
  8. public partial class SonopostService : ServiceBase
  9. {
  10. private string _sonopostPath;
  11. private ProcessKeepHelper _keepHelper;
  12. public SonopostService()
  13. {
  14. InitializeComponent();
  15. }
  16. protected override void OnStart(string[] args)
  17. {
  18. try
  19. {
  20. Thread.Sleep(2000);
  21. try
  22. {
  23. ProcessHelper.FinishProcessByName("Vinno.FIS.Sonopost.Upgrade");
  24. ProcessHelper.FinishProcessByName("Sonopost");
  25. ProcessHelper.FinishProcessByName("Vinno.FIS.Sonopost.TRTCClient");
  26. }
  27. catch (Exception e)
  28. {
  29. WriteLog(e.ToString());
  30. }
  31. _sonopostPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Sonopost.exe");
  32. var processId = ProcessHelper.StartProcess(_sonopostPath);
  33. _keepHelper = new ProcessKeepHelper(processId);
  34. _keepHelper.StartKeepProcess(() =>
  35. {
  36. return ProcessHelper.StartProcess(_sonopostPath);
  37. });
  38. }
  39. catch (Exception e)
  40. {
  41. WriteLog(e.ToString());
  42. }
  43. }
  44. protected override void OnStop()
  45. {
  46. _keepHelper?.StopKeepProcess();
  47. base.OnStop();
  48. }
  49. private void WriteLog(string msg)
  50. {
  51. var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SonopostServiceLog.log");
  52. File.AppendAllText(path, msg);
  53. }
  54. }
  55. }