Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 跟踪其他应用程序的运行时间_C#_.net_Vb.net_Windows_Winforms - Fatal编程技术网

C# 跟踪其他应用程序的运行时间

C# 跟踪其他应用程序的运行时间,c#,.net,vb.net,windows,winforms,C#,.net,Vb.net,Windows,Winforms,我正在考虑构建一个应用程序来记录一个程序/应用程序在windows机器上运行了多长时间 应用程序将在windows登录时启动,并从那里通过收集开始和结束时间记录所有程序的使用时间 我正在寻找一种替代方法,最好是事件驱动的,以捕获程序的开始时间和结束时间,而不轮询WMI,因为对于我需要的小信息来说,它似乎真的需要大量的CPU 如果有人知道我可以使用什么其他方法来捕获流程的开始和结束时间,请提出您的想法 发现此主题它使用WMI,但似乎相当受事件驱动。我将尝试此主题,并报告我关于性能的发现 您可以使用

我正在考虑构建一个应用程序来记录一个程序/应用程序在windows机器上运行了多长时间

应用程序将在windows登录时启动,并从那里通过收集开始和结束时间记录所有程序的使用时间

我正在寻找一种替代方法,最好是事件驱动的,以捕获程序的开始时间和结束时间,而不轮询WMI,因为对于我需要的小信息来说,它似乎真的需要大量的CPU

如果有人知道我可以使用什么其他方法来捕获流程的开始和结束时间,请提出您的想法

发现此主题它使用WMI,但似乎相当受事件驱动。我将尝试此主题,并报告我关于性能的发现

您可以使用Process.getprocesss()获取所有正在运行的进程,然后注册到退出事件:

    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;


    }

现在,您所要做的就是管理流程列表……

谢谢您,流程。退出的事件正是我想要的。使用Process.Exited事件和新进程的WMI事件,我可以收集每个应用程序的开始和结束时间。