如何检查进程是否空闲?C# [英] How to check if process is idle? C#

查看:67
本文介绍了如何检查进程是否空闲?C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说.

我正在寻找一种方法来检查进程是否空闲,有明显的运行和未运行,但如何确定它是否没有执行任何操作?

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?

谢谢

推荐答案

这取决于你如何定义空闲.

it depends on how you define idle.

但是,您可以创建某种启发式方法来使用 Process 类将进程定义为空闲.如果一个进程在一段时间内没有消耗超过 thresholdMillis,我会假设它是空闲的"

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
}

因此,根据您选择 threshold_millis 值的方式,您将获得不同的结果.但这应该是一个很好的启发式方法,可以查看进程是否空闲.

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.

这篇关于如何检查进程是否空闲?C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆