软件编程
位置:首页>> 软件编程>> C#编程>> C#实现软件监控外部程序运行状态的方法

C#实现软件监控外部程序运行状态的方法

作者:shichen2014  发布时间:2023-12-04 20:46:15 

标签:C#,监控,方法

本文实例讲述了C#实现软件监控外部程序运行状态的方法。分享给大家供大家参考。具体方法如下:

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

引用的类

using System.Diagnostics;//引入Process 类

声明

private Process[] MyProcesses;

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

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
    }
}
 
private void myprocess_Exited(object sender, EventArgs e)//被触发的程序
{
    MessageBox.Show("SajetManager close");
}

希望本文所述对大家的C#程序设计有所帮助。

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com