当前位置: 移动技术网 > IT编程>开发语言>c# > 如何利用c#实现通用守护进程

如何利用c#实现通用守护进程

2020年03月09日  | 移动技术网IT编程  | 我要评论
1. 下载   源码下载:http://files.cnblogs.com/tianzhiliang/cocowatcher.rar   安装包下载:http://files.cn

1. 下载

  源码下载:

  安装包下载:

       本地下载:

2. 安装注意事项

在配置档中配置你要守护的应用程序,应用程序之间用逗号隔开:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <appsettings>
 <add key="processaddress" value="
 d:\war3.exe,
 d:\note.txt,
 d:\girl.jpg
 " />
 </appsettings>
</configuration>

该项目是windows服务,直接打开“cocowatcher.exe”会报错,如图1所示:

运行该守护程序需安装windows服务,点击批处理文档“安装.bat”即可,“安装.bat”具体内容如下:

"%cd%\installutil.exe" "%cd%\cocowatcher.exe"
net start "cocowatcher"
pause

如果你想卸载该守护程序,点击批处理文档“卸载.bat”,“卸载.bat”具体内容如下:

net stop "cocowatcher"
"%cd%\installutil.exe" "%cd%\cocowatcher.exe" -u
taskkill /f /im cocowatcher.exe
pause

3. 需求分析

  用户指定要守护的应用程序(数量不限),该应用程序不仅包括exe可执行文件,还包括诸如jpg、txt等所有能双击打开执行的应用程序。用户设定好要守护的应用程序后,关闭应用程序(包括合法和非法关闭),该应用程序要能立即重启打开。当电脑重启时,要守护的应用程序也能自动全部打开。

4. 详细设计

  要实现上述需求,首先要提供一个配置档,让用户能随意配置要守护的应用程序。那么,该配置档要配置应用程序的什么信息呢?答案:应用程序的全路径。

  好,我们已经知道了要守护的应用程序的全路径,接下来怎样完成守护任务呢?首先,我们应该打开任务管理器,查看一下正在运行的有哪些进程,然后逐一读取出这些进程的全路径,与要守护的应用程序的全路径比对,如果一致,说明要守护的应用程序已开启了,此时要分配一条线程监控该进程句柄,当该进程句柄返回信息,说明该进程已关闭,此时释放进程句柄内存,并重启该进程。如果遍历任务管理进程列表中所有进程,没有找到与要守护的应用程序的全路径一致的进程,说明要守护的应用程序尚未打开,此时要启动该应用程序,然后转入监控流程。

  值得注意的是,一定要额外分配线程去监控要守护的应用程序,为什么?因为如果你用主线程(入口函数线程)去执行监控任务,会被长期阻塞,直到进程退出才会被激活,这样就无法运行后续程序。况且,监控程序要实现持续监控,要使用死循环,如果主线程进入死循环,就无法监控其他要守护的进程了。

5. 代码详解

  windows服务的开发步骤,请参考msdn,此处略去。下面将关键代码贴出,加以解释。

  读取配置档中“processaddress”节点,获取要守护的应用程序全目录,验证应用程序全目录,如果合法,进入扫描任务管理器进程列表流程。

/// <summary>
/// 开始监控
/// </summary>
private void startwatch()
{
 if (this._processaddress != null)
 {
 if (this._processaddress.length > 0)
 {
 foreach (string str in _processaddress)
 {
 if (str.trim() != "")
 {
  if (file.exists(str.trim()))
  {
  this.scanprocesslist(str.trim());
  }
 }
 }
 }
 }
}

  打开任务管理器,查看一下正在运行的有哪些进程,然后逐一读取出这些进程的全路径,与要守护的应用程序的全路径比对,如果一致,说明要守护的应用程序已开启了,进入监控流程。如果遍历任务管理进程列表中所有进程,没有找到与要守护的应用程序的全路径一致的进程,说明要守护的应用程序尚未打开,此时要启动该应用程序,然后转入监控流程。

/// <summary>
/// 扫描进程列表,判断进程对应的全路径是否与指定路径一致
/// 如果一致,说明进程已启动
/// 如果不一致,说明进程尚未启动
/// </summary>
/// <param name="straddress"></param>
private void scanprocesslist(string address)
{
 process[] arrayprocess = process.getprocesses();
 foreach (process p in arrayprocess)
 {
 //system、idle进程会拒绝访问其全路径
 if (p.processname != "system" && p.processname != "idle")
 {
 try
 {
 if (this.formatpath(address) == this.formatpath(p.mainmodule.filename.tostring()))
 {
  //进程已启动
  this.watchprocess(p, address);
  return;
 }
 }
 catch
 {
 //拒绝访问进程的全路径
 this.savelog("进程(" + p.id.tostring() + ")(" + p.processname.tostring() + ")拒绝访问全路径!");
 }
 }
 }
 
 //进程尚未启动
 process process = new process();
 process.startinfo.filename = address;
 process.start();
 this.watchprocess(process, address);
}

  分配一条线程,执行监控任务:

 /// <summary>
 /// 监听进程
 /// </summary>
 /// <param name="p"></param>
 /// <param name="address"></param>
 private void watchprocess(process process, string address)
 {
 processrestart objprocessrestart = new processrestart(process, address);
 thread thread = new thread(new threadstart(objprocessrestart.restartprocess));
 thread.start();
 }
 
 
public class processrestart
{
 //字段
 private process _process;
 private string _address;
 
 
 /// <summary>
 /// 构造函数
 /// </summary>
 public processrestart()
 {}
 
 
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="process"></param>
 /// <param name="address"></param>
 public processrestart(process process, string address)
 {
 this._process = process;
 this._address = address;
 }
 
 
 /// <summary>
 /// 重启进程
 /// </summary>
 public void restartprocess()
 {
 try
 {
 while (true)
 {
 this._process.waitforexit();
 this._process.close(); //释放已退出进程的句柄
 this._process.startinfo.filename = this._address;
 this._process.start();
 
 thread.sleep(1000);
 }
 }
 catch (exception ex)
 {
 processwatcher objprocesswatcher = new processwatcher();
 objprocesswatcher.savelog("restartprocess() 出错,监控程序已取消对进程("
 + this._process.id.tostring() +")(" + this._process.processname.tostring() 
 + ")的监控,错误描述为:" + ex.message.tostring());
 }
 }
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网