| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using System.Windows.Forms;
- using BeiJingComService.Hosts;
- using BeiJingComService.Infrastructure;
- using BeiJingComService.Protocols;
- namespace BeiJingComService
- {
- static class Program
- {
- [STAThread]
- static void Main(string[] args)
- {
- string mode = args.Length > 0 ? args[0].ToLowerInvariant() : "tray";
- // 托盘与服务使用不同的互斥体,避免互相抢占,更不允许互相结束进程
- string mutexName = mode == "--service"
- ? @"Global\BeiJingComService_Service_SingleInstance"
- : @"Global\BeiJingComService_Tray_SingleInstance";
- using (var guard = AcquireSingleInstanceGuard(mode, mutexName))
- {
- if (guard == null) return;
- var protocols = new ITransportProtocol[] { new HttpProtocol(AppConfig.HttpPort) };
- try
- {
- switch (mode)
- {
- case "--install":
- ServiceApplicationHost.Install();
- break;
- case "--uninstall":
- ServiceApplicationHost.Uninstall();
- break;
- case "--service":
- using (var serviceHost = new ServiceApplicationHost(protocols))
- {
- serviceHost.Start();
- }
- break;
- default:
- using (var trayHost = new TrayApplicationHost(protocols))
- {
- trayHost.Start();
- }
- break;
- }
- }
- catch (Exception ex)
- {
- Logger.Error("宿主运行异常", ex);
- if (mode == "tray")
- {
- MessageBox.Show($"服务运行异常: {ex.Message}", "错误",
- MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- }
- }
- private static SingleInstanceGuard AcquireSingleInstanceGuard(string mode, string mutexName)
- {
- var guard = new SingleInstanceGuard(mutexName);
- if (guard.IsOwned) return guard;
- Logger.Info($"检测到已有实例在运行(模式: {mode})");
- if (mode != "tray")
- {
- guard.Dispose();
- return null;
- }
- var result = MessageBox.Show(
- "本地 COM 通讯服务已经在运行。是否终止已有实例并重新启动?",
- "提示",
- MessageBoxButtons.YesNo,
- MessageBoxIcon.Question);
- if (result != DialogResult.Yes)
- {
- guard.Dispose();
- return null;
- }
- KillExistingInstance();
- guard.Dispose();
- for (int i = 0; i < 10; i++)
- {
- Thread.Sleep(300);
- guard = new SingleInstanceGuard(mutexName);
- if (guard.IsOwned) return guard;
- guard.Dispose();
- }
- MessageBox.Show(
- "无法获取实例控制权,请手动在任务管理器中结束 BeiJingComService 进程后重试。",
- "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return null;
- }
- private static void KillExistingInstance()
- {
- string processName = Path.GetFileNameWithoutExtension(Application.ExecutablePath);
- int currentId = Process.GetCurrentProcess().Id;
- var processes = Process.GetProcessesByName(processName)
- .Where(p => p.Id != currentId)
- .ToArray();
- foreach (var process in processes)
- {
- try
- {
- Logger.Info($"准备结束已有进程: {process.Id}");
- process.Kill();
- if (!process.WaitForExit(3000))
- {
- Logger.Error($"进程 {process.Id} 未在 3 秒内退出");
- }
- }
- catch (Exception ex)
- {
- Logger.Error($"结束进程 {process.Id} 失败", ex);
- }
- }
- }
- }
- }
|