一、小工具的用处:
1.可以保证应用程序始终处于运行状态,确保其提供服务的稳定性和可靠性
2.在发生异常或崩溃时,可以自动重启应用程序,避免因程序崩溃而导致业务中断
3.实现无人化程序的自动运行
二、如果要模拟程序崩溃可以用这句代码
Environment.FailFast("程序意外终止");
using System.Collections.Generic; using System.Threading; static void Main(string[] args) { var monitorThread = new Thread(MonitorApplication); monitorThread.Start(); Console.WriteLine("程序已启动..."); // 启动目标应用程序 注:以下是我的测试路径 请替换为你的应用程序的路径 var targetProcessPath = @"C:\Users\34349\Desktop\Storage\Storage\bin\Debug\Storage.exe"; var targetProcessStartInfo = new ProcessStartInfo(targetProcessPath) { CreateNoWindow = true, UseShellExecute = false }; Process.Start(targetProcessStartInfo); // 等待监控线程结束 monitorThread.Join(); } static void MonitorApplication() { //注:以下是我的测试进程名称 请替换为你的应用程序的进程名称 var targetProcessName = "Storage"; while (true) { var processes = Process.GetProcessesByName(targetProcessName); if (processes.Length == 0) { Console.WriteLine("程序已崩溃正在重新启动..."); // 目标应用程序已退出,重新启动 StartTargetApplication(); } Thread.Sleep(1000); // 每隔一段时间检查一次 目前设置的是1秒 可自行更改 } } static void StartTargetApplication() { //注:以下是我的测试路径 请替换为你的应用程序的路径 var targetProcessPath = @"C:\Users\34349\Desktop\Storage\Storage\bin\Debug\Storage.exe"; var targetProcessStartInfo = new ProcessStartInfo(targetProcessPath) { CreateNoWindow = true, UseShellExecute = false }; Process.Start(targetProcessStartInfo); }