Sandeep Bansal
Sandeep Bansal

Reputation: 6394

How to check if process is idle? C#

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

Upvotes: 0

Views: 9858

Answers (2)

luke
luke

Reputation: 14788

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.

Upvotes: 6

Blindy
Blindy

Reputation: 67487

Are you looking for something like GetLastInputInfo?

Upvotes: 0

Related Questions