110,538
社区成员
发帖
与我相关
我的任务
分享
public void MonitorProcess()
{
// Create the source, if it does not already exist.
if (!EventLog.SourceExists("MySource"))
{
EventLog.CreateEventSource("MySource", "MyNewLog");
}
EventLog myLog = new EventLog();
myLog.Source = "MySource";
List<String> CurrentSystemProcessList = new List<String>();
CurrentSystemProcessList.Clear();
Process[] localAll = Process.GetProcesses();
//EventLog myLog = new EventLog("ServiceTest", ".", "ServiceTest");
StringBuilder msg = new StringBuilder("");
for (int i = 0; i < localAll.Length; i++)
{
if (localAll[i].ProcessName == "Rever")
{
//localAll[i].Close();
try
{
bool IsCloseMainWindow = false;
IsCloseMainWindow=localAll[i].CloseMainWindow();
if(!IsCloseMainWindow)
{
msg.Remove(0, msg.Length);
msg.Append(DateTime.Now.ToString());
msg.Append(" 进程调用CloseMainWindow,没有成功!");
myLog.WriteEntry(msg.ToString());
localAll[i].Kill();
}
localAll[i].WaitForExit();
//this.Stop();
Process.Start(@"D:\Rever.exe");
msg.Remove(0, msg.Length);
msg.Append(DateTime.Now.ToString());
msg.Append(" 程序执行成功!");
myLog.WriteEntry(msg.ToString());
}
catch (Win32Exception ee)
{
msg.Remove(0, msg.Length);
msg.Append(DateTime.Now.ToString());
msg.Append(" 退出程序失败!");
myLog.WriteEntry(msg.ToString());
}
}
}
}