我们有几个遗留程序(vb6)生成excel报告。它们是在服务器上运行的计划任务。是的,违反了这个:http://support.microsoft.com/kb/257757
结果,我们在服务器上获得了许多“死”的Excel实例。我打算写一个程序来清理这些死的excels。杀死空闲实例的正确方法是什么?我知道这不是一个好习惯,但对我来说似乎是最简单的方法。这是我的代码:
Process[] procs = Process.GetProcessesByName("EXCEL");
List<ProcessInfo> beginProcessInfoList = new List<ProcessInfo>();
foreach (Process p in procs)
{
if (p.HasExited)
continue;
try
{
ProcessInfo pi = new ProcessInfo() { PID = p.Id, TotalProcessorTime = p.TotalProcessorTime };
beginProcessInfoList.Add(pi);
}
catch (Exception exp)
{
this.Log(exp.Message);
}
}
Thread.Sleep(TimeSpan.FromSeconds(10));
foreach (Process p in procs)
{
if (p.HasExited)
continue;
try
{
p.Refresh();
if (p.TotalProcessorTime - beginProcessInfoList.Single(pi => pi.PID == p.Id).TotalProcessorTime == TimeSpan.FromSeconds(0))
{
p.Kill();
p.WaitForExit();
this.Log(string.Format("EXCEL started at {0} killed at {1}.", p.StartTime, DateTime.Now));
}
}
catch (Exception exp)
{
this.Log(exp.Message);
}
}