この記事はネチズンが投稿した。
著者:高慢と偏見
前の記事:C#を使ってウォッチドッグプログラムを簡単に作る
原文へのリンク:https//www.cnblogs.com/chonglu/p/16913746.html
まず、インターネットのサポートに感謝します。

欢迎网友们投稿技术类文章,题材不限,没有稿费的哈...

概要概要概要
いくつかの特別なプロジェクトでは、ソフトウェアが無人である可能性があります。プログラムが不可解にハングアップしたり、プロセスが削除されたりした場合、ウォッチドッグプログラムの開発は非常に必要です。それは死んでいない小さな男のようなものです。プログラムが正常に終了しない限り、すぐにプログラムを起動することができます。
コードの実装
ヒント:最後に完全なソースコードがあるので、ステップバイステップではありません。
犬クラスを作成し、主に介護者プログラムがまだ実行されているかどうかを間隔でスキャンします。
开了个定时器,每 5 秒去检查 1 次,如果没有找到进程则使用Process启动程序
public class Dog
{
private Timer timer = new Timer();
private string processName ;
private string filePath;//要监控的程序的路径
public Dog()
{
timer.Interval = 5000;
timer.Tick += timer_Tick;
}
public void Start(string filePath)
{
this.filePath = filePath;
this.processName = Path.GetFileNameWithoutExtension(filePath);
timer.Enabled = true;
}
/// <summary>
/// 定时检测系统是否在运行
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer_Tick(object sender, EventArgs e)
{
try
{
Process[] myproc = Process.GetProcessesByName(processName);
if (myproc.Length == 0)
{
Log.Info("检测到看护程序已退出,开始重新激活程序,程序路径:{0}",filePath);
ProcessStartInfo info = new ProcessStartInfo
{
WorkingDirectory = Path.GetDirectoryName(filePath),
FileName = filePath,
UseShellExecute = true
};
Process.Start(info);
Log.Info("看护程序已启动");
}
}
catch (Exception)
{
}
}
}
2、在程序入口接收被看护程序的路径,启动Dog扫描
static class Program
{
static NotifyIcon icon = new NotifyIcon();
private static Dog dog = new Dog();
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
if (args == null || args.Length == 0)
{
MessageBox.Show("启动参数异常", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
string filePath = args[0];
if(!File.Exists(filePath))
{
MessageBox.Show("启动参数异常", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//遍历与当前进程名称相同的进程列表
foreach (Process process in processes)
{
//如果实例已经存在则忽略当前进程
if (process.Id != current.Id)
{
//保证要打开的进程同已经存在的进程来自同一文件路径
if (process.MainModule.FileName.Equals(current.MainModule.FileName))
{
//已经存在的进程
return;
}
else
{
process.Kill();
process.WaitForExit(3000);
}
}
}
icon.Text = "看门狗";
icon.Visible = true;
Log.Info("启动看门狗,看护程序:{0}",filePath);
dog.Start(filePath);
Application.Run();
}
}
3、ロガーの簡単な実装(サードパーティ製ライブラリを使用しても、ケアプログラムは依存関係を持たないことをお勧めします)、また、以下の私を直接使用することができます、非常に簡単で、依存関係なしです。
public class Log
{
//读写锁,当资源处于写入模式时,其他线程写入需要等待本次写入结束之后才能继续写入
private static ReaderWriterLockSlim LogWriteLock = new ReaderWriterLockSlim();
//日志文件路径
public static string logPath = "logs\\dog.txt";
//静态方法todo:在处理话类型之前自动调用,去检查日志文件是否存在
static Log()
{
//创建文件夹
if (!Directory.Exists("logs"))
{
Directory.CreateDirectory("logs");
}
}
/// <summary>
/// 写入日志.
/// </summary>
public static void Info(string format, params object[] args)
{
try
{
LogWriteLock.EnterWriteLock();
string msg = args.Length > 0 ? string.Format(format, args) : format;
using (FileStream stream = new FileStream(logPath, FileMode.Append))
{
StreamWriter write = new StreamWriter(stream);
string content = String.Format("{0} {1}",DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),msg);
write.WriteLine(content);
//关闭并销毁流写入文件
write.Close();
write.Dispose();
}
}
catch (Exception e)
{
}
finally
{
LogWriteLock.ExitWriteLock();
}
}
}
これで介護手続きが完了しました。次に、メインプログラム(被介護者プログラム)で開始停止クラスをカプセル化します。
4、メインプログラムパッケージウォッチドッグスタートストップクラス
public static class WatchDog
{
private static string processName = "WatchDog"; //看护程序进程名(注意这里不是被看护程序名,你可以试一下换成主程序名字会使什么效果)
private static string appPath = AppDomain.CurrentDomain.BaseDirectory; //系统启动目录
/// <summary>
/// 启动看门狗
/// </summary>
public static void Start()
{
try
{
string program = string.Format("{0}{1}.exe", appPath, processName);
ProcessStartInfo info = new ProcessStartInfo
{
WorkingDirectory = appPath,
FileName = program,
CreateNoWindow = true,
UseShellExecute = true,
Arguments = Process.GetCurrentProcess().MainModule.FileName //被看护程序的完整路径
};
Process.Start(info);
}
catch (Exception)
{
}
}
/// <summary>
/// 停用看门狗
/// </summary>
public static void Stop()
{
Process[] myproc = Process.GetProcessesByName(processName);
foreach (Process pro in myproc)
{
pro.Kill();
pro.WaitForExit(3000);
}
}
}
原理は非常に簡単で、2つの点に注意が必要です。
processName字段表示看护程序,不是被看护程序,如果写反了,嗯...(你可以试下效果)Arguments参数是被看护程序的完整路径,因为一般情况下,是由被看护程序启动看护程序,所以我们可以直接使用Process.GetCurrentProcess().MainModule.FileName获取到被看护程序的完整路径
メインプログラムのエントリポイントでウォッチドッグを起動します
public partial class App : Application
{
[STAThread]
static void Main()
{
//程序启动前调用看护程序
WatchDog.Start();
Application app = new Application();
MainWindow mainWindow = new MainWindow();
app.Run(mainWindow);
}
}
Winform、通常のWPF、Prismなどのエントリポイントは異なり、プロジェクトの実際の状況に応じて柔軟に処理できます。
最後に、通常の終了が必要な場所(メインプログラムのシャットダウンボタンなど、通常の終了が必要な場所)でウォッチドッグを停止します。
効果は
