一、打开Visual Studio,创建项目->Windows 服务(.NET Framework)
二、点击Service.cs 点击切换到代码视图
static Timer Timer; private Thread monitorThread; private static string logFilePath; private static Process winFormsProcess; public Service1() { InitializeComponent(); logFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log.txt"); } protected override void OnStart(string[] args) { // 启动监控线程 monitorThread = new Thread(MonitorApplication); monitorThread.Start(); // 设置定时器,每隔10秒检查一次目标应用程序是否退出 Timer = new Timer(p => MonitorAndRestart(), null, TimeSpan.Zero, TimeSpan.FromSeconds(10)); //启动目标程序 StartTargetApplication(); } static void MonitorAndRestart() { var targetProcessName = "FR"; // 替换为你的目标应用程序的进程名称