tags:

views:

251

answers:

2

As the title says.

I am looking for a way to check if a process is idle, there is the obvious running and not running but how to determine if it's not doing anything?

Thanks

A: 

Are you looking for something like GetLastInputInfo?

Blindy
+2  A: 

it depends on how you define idle.

However you could create some sort of heuristic for defining a process as idle using the Process class. I'll assume that a process is 'idle' if it hasn't consumed more than thresholdMillis over a certain period of time

Process p = Process.GetProcessById(proc_id);
TimeSpan begin_cpu_time = p.TotalProcessorTime;
//... wait a while
p.Refresh();
TimeSpan end_cpu_time = p.TotalProcessorTime;
if(end_cpu_time - begin_cpu_time < TimeSpan.FromMillis(thresholdMillis))
{
    //..process is idle
}
else
{
    //..process is not idle
}

so depending on how you choose your threshold_millis value you will get different results. but this should be a decent heuristic for seeing if a process is idle.

Ideally you would probably use some sort of timer to periodically query and update the 'idleness' of a process.

luke
Just the idea I was looking for.Thanks
Sandeep Bansal