How to check if process is idle? C#

前端 未结 2 1104
无人共我
无人共我 2020-12-22 12:22

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 anythin

相关标签:
2条回答
  • 2020-12-22 12:43

    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.

    0 讨论(0)
  • 2020-12-22 12:48

    Are you looking for something like GetLastInputInfo?

    0 讨论(0)
提交回复
热议问题