Process monitoring

Asked

Viewed 391 times

1

I’m developing a process monitoring application, to get the running programs I’m using this class: Win32_process, but I am not able to find a method to discount idle time, for example I am only able to mention the total time that the process has been open, I would not consider the time that this process has been minimized or stopped.

1 answer

1

Win32_Process has the property CreationDate, that despite the name is a Datetime. To know the idle time of a program, just discover the total time and subtract the user’s time (UserModeTime) and time spent core (KernelModeTime)

An example, using the standard C classes#

    public static void Main( string[] args )
    {
        var procs = Process.GetProcesses().Where( x => x.ProcessName.Contains( "chrome" ) ).ToList();
        foreach ( var proc in procs )
        {
            var timeTotal = DateTime.Now - proc.StartTime;
            var timeProc = proc.TotalProcessorTime;
            var timeIdle = timeTotal.Subtract( timeProc );

            Console.WriteLine( proc.Id + " total " + timeTotal + " user+kernel " + timeProc + " idle " + timeIdle );
        }
    }

It produces things like:

2570 total 1.18:52:22.8433460 user+kernel 00:48:02 idle 1.18:04:20.843346000:11:57.9100000

Browser other questions tagged

You are not signed in. Login or sign up in order to post.