这取决于你如何定义空闲。
但是,您可以创建某种启发式方法,使用 Process 类将进程定义为空闲。如果一个进程在一段时间内没有消耗超过thresholdMillis ,我会假设它是“空闲”的
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
}
Run Code Online (Sandbox Code Playgroud)
因此,根据您选择的threshold_millis值的不同,您将得到不同的结果。但这应该是一个不错的启发式方法,用于查看进程是否空闲。
理想情况下,您可能会使用某种计时器来定期查询和更新进程的“空闲”状态。
| 归档时间: |
|
| 查看次数: |
9563 次 |
| 最近记录: |