需要外挂一个程序,用于监控另一个程序运行状态,一旦检测到另一程序关闭,就触发一个事件做其他处理。
引用的类
? 1
class="java plain" style="margin: 0px !important; padding: 0px !important; outline: 0px !important; border-radius: 0px !important; border: 0px currentColor !important; left: auto !important; top: auto !important; width: auto !important; height: auto !important; text-align: left !important; right: auto !important; bottom: auto !important; color: black !important; line-height: 1.1em !important; overflow: visible !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 1em !important; font-style: normal !important; font-weight: normal !important; vertical-align: baseline !important; float: none !important; position: static !important; min-height: inherit !important; box-sizing: content-box !important; background-image: none !important;">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"
);
}