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 + " ";
    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");
}
原文地址:https://www.cnblogs.com/gc2013/p/4164687.html