BaseHandler.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. 
  2. using FisTools;
  3. using System;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Threading;
  7. namespace fis.Loader.Handlers
  8. {
  9. /// <summary>
  10. /// 命令处理器基类
  11. /// </summary>
  12. internal abstract class BaseHandler
  13. {
  14. private int _currentProcessId;
  15. internal BaseHandler(LaunchMethodEnum method)
  16. {
  17. Method = method;
  18. }
  19. protected int CurrentProcessId
  20. {
  21. get {
  22. if (_currentProcessId == 0)
  23. {
  24. var currentProcess = Process.GetCurrentProcess();
  25. _currentProcessId = currentProcess.Id;
  26. }
  27. return _currentProcessId;
  28. }
  29. }
  30. protected void KillDamon()
  31. {
  32. ToolManager.Instance.LoaderCenter.KillProcessWithGivenName("loader", false);
  33. }
  34. protected void StartDamon(string fileName,string fisFileDir)
  35. {
  36. try {
  37. if (IsCartSystemVersion)
  38. {
  39. while (true)
  40. {
  41. var process = Process.GetProcessesByName("fis").FirstOrDefault();
  42. var loaderCount = Process.GetProcessesByName("loader").Length;
  43. if (process == null && loaderCount==1)
  44. {
  45. ToolManager.Instance.LoaderCenter.StartProcessWithWorkingDirectory(fileName, fisFileDir);
  46. }
  47. Thread.Sleep(3000);
  48. }
  49. }
  50. }
  51. catch (Exception ex)
  52. {
  53. Logger.WriteLine($"Error happened while start damon {ex}");
  54. }
  55. }
  56. protected bool IsCartSystemVersion
  57. {
  58. get
  59. {
  60. var cartSystem = Environment.GetEnvironmentVariable("CartSystem");
  61. if (!string.IsNullOrWhiteSpace(cartSystem))
  62. {
  63. return true;
  64. }
  65. return false;
  66. }
  67. }
  68. /// <summary>
  69. /// 启动方式
  70. /// </summary>
  71. internal LaunchMethodEnum Method { get; private set; }
  72. /// <summary>
  73. /// 执行命令
  74. /// </summary>
  75. internal abstract void Execute();
  76. protected void KillApp()
  77. {
  78. ToolManager.Instance.LoaderCenter.KillProcessWithGivenName("fis");
  79. }
  80. }
  81. }