C#软件监控外部程序运行状态


需要外挂一个程序,用于监控另一个程序运行状态,一旦检测到另一程序关闭,就触发一个事件做其他处理。

 

引用的类

 

?
1
using System.Diagnostics; //引入Process 类


声明

 

 

?
1
private Process[] MyProcesses;


主要处理部分,该段代码可放在定时器中循环检测监控的程序是否启动

 

 

?
1
2
3
4
5
6
7
8
9
10
11
MyProcesses = Process.GetProcessesByName( "SajetManager" ); //需要监控的程序名,该方法带出该程序所有用到的进程
foreach (Process myprocess in MyProcesses)
{
     textBox1.Text += myprocess.ProcessName + "\r\n" ;
     if (myprocess.ProcessName.ToLower() == "sajetmanager" )
     {
         MessageBox.Show( "SajetManager" );
         myprocess.EnableRaisingEvents = true ; //设置进程终止时触发的时间
         myprocess.Exited += new EventHandler(myprocess_Exited); //发现外部程序关闭即触发方法myprocess_Exited
     }
}

 

?
1
2
3
4
private void myprocess_Exited(object sender, EventArgs e) //被触发的程序
{
     MessageBox.Show( "SajetManager close" );
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM