我正在构建一个应用程序来记录程序/应用程序在整个正常运行时间内在Windows机器上运行的时间。
应用程序将在Windows登录时启动,并从那里通过收集开始和结束时间记录所有程序使用时间。
我正在寻找一种替代方法,最好是事件驱动来捕获程序开始时间和结束时间而不轮询WMI,因为对于我需要的小信息来说它似乎真的是CPU密集型的。
如果有人知道我可以使用哪些其他方法来捕获流程的开始和结束时间,请提出您的想法。
发现这个主题它使用了WMI,但似乎是事件驱动的,我会试一试并报告我对性能的发现 How to monitor that an application is opened?
答案 0 :(得分:0)
你可以使用Process.GetProcesses()获取所有正在运行的进程,然后注册到那里退出事件:
static void Main(string[] args)
{
var p = Process.GetProcesses();
foreach (var process in p)
{
try
{
// you can add the process to your process list, chack existence and etc...
process.EnableRaisingEvents = true;
process.Exited += Program_Exited;
}
catch (Exception)
{
//any process that you don't have credantial will throw exception...
}
}
Console.ReadLine();
}
private static void Program_Exited(object sender, EventArgs e)
{
var process = (Process) sender;
var startTime = process.StartTime;
var endTime = process.ExitTime;
}
现在你所要做的就是管理进程列表......