Program.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Windows.Forms;
  7. using BeiJingComService.Hosts;
  8. using BeiJingComService.Infrastructure;
  9. using BeiJingComService.Protocols;
  10. namespace BeiJingComService
  11. {
  12. static class Program
  13. {
  14. [STAThread]
  15. static void Main(string[] args)
  16. {
  17. string mode = args.Length > 0 ? args[0].ToLowerInvariant() : "tray";
  18. // 托盘与服务使用不同的互斥体,避免互相抢占,更不允许互相结束进程
  19. string mutexName = mode == "--service"
  20. ? @"Global\BeiJingComService_Service_SingleInstance"
  21. : @"Global\BeiJingComService_Tray_SingleInstance";
  22. using (var guard = AcquireSingleInstanceGuard(mode, mutexName))
  23. {
  24. if (guard == null) return;
  25. var protocols = new ITransportProtocol[] { new HttpProtocol(AppConfig.HttpPort) };
  26. try
  27. {
  28. switch (mode)
  29. {
  30. case "--install":
  31. ServiceApplicationHost.Install();
  32. break;
  33. case "--uninstall":
  34. ServiceApplicationHost.Uninstall();
  35. break;
  36. case "--service":
  37. using (var serviceHost = new ServiceApplicationHost(protocols))
  38. {
  39. serviceHost.Start();
  40. }
  41. break;
  42. default:
  43. using (var trayHost = new TrayApplicationHost(protocols))
  44. {
  45. trayHost.Start();
  46. }
  47. break;
  48. }
  49. }
  50. catch (Exception ex)
  51. {
  52. Logger.Error("宿主运行异常", ex);
  53. if (mode == "tray")
  54. {
  55. MessageBox.Show($"服务运行异常: {ex.Message}", "错误",
  56. MessageBoxButtons.OK, MessageBoxIcon.Error);
  57. }
  58. }
  59. }
  60. }
  61. private static SingleInstanceGuard AcquireSingleInstanceGuard(string mode, string mutexName)
  62. {
  63. var guard = new SingleInstanceGuard(mutexName);
  64. if (guard.IsOwned) return guard;
  65. Logger.Info($"检测到已有实例在运行(模式: {mode})");
  66. if (mode != "tray")
  67. {
  68. guard.Dispose();
  69. return null;
  70. }
  71. var result = MessageBox.Show(
  72. "本地 COM 通讯服务已经在运行。是否终止已有实例并重新启动?",
  73. "提示",
  74. MessageBoxButtons.YesNo,
  75. MessageBoxIcon.Question);
  76. if (result != DialogResult.Yes)
  77. {
  78. guard.Dispose();
  79. return null;
  80. }
  81. KillExistingInstance();
  82. guard.Dispose();
  83. for (int i = 0; i < 10; i++)
  84. {
  85. Thread.Sleep(300);
  86. guard = new SingleInstanceGuard(mutexName);
  87. if (guard.IsOwned) return guard;
  88. guard.Dispose();
  89. }
  90. MessageBox.Show(
  91. "无法获取实例控制权,请手动在任务管理器中结束 BeiJingComService 进程后重试。",
  92. "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  93. return null;
  94. }
  95. private static void KillExistingInstance()
  96. {
  97. string processName = Path.GetFileNameWithoutExtension(Application.ExecutablePath);
  98. int currentId = Process.GetCurrentProcess().Id;
  99. var processes = Process.GetProcessesByName(processName)
  100. .Where(p => p.Id != currentId)
  101. .ToArray();
  102. foreach (var process in processes)
  103. {
  104. try
  105. {
  106. Logger.Info($"准备结束已有进程: {process.Id}");
  107. process.Kill();
  108. if (!process.WaitForExit(3000))
  109. {
  110. Logger.Error($"进程 {process.Id} 未在 3 秒内退出");
  111. }
  112. }
  113. catch (Exception ex)
  114. {
  115. Logger.Error($"结束进程 {process.Id} 失败", ex);
  116. }
  117. }
  118. }
  119. }
  120. }