我正在编写一个C#应用程序,它需要能够告诉某个应用程序打开需要多长时间.我使用秒表类作为我的计时器.开始时间很简单,因为我完全使用运行.exe的调用来设置它.问题是找出程序打开时的时间.我唯一能想到的就是使用一个PerformanceCounter对象并使用while循环检查CPU%是否小于某个数字.
目前我正在使用PerformanceCounter,但我没有运气显示CPU%(它总是显示0).我的猜测是,应用程序打开的速度比PerformanceCounter可以检查CPU%的速度快,或者PerformanceCounter没有看到进程名称,因为我调用的速度太快(我非常怀疑后者是因为如果发生这种情况,我想我会得到错误).
还有其他方法可以解决这个问题吗?有没有什么我可能做错了,一直给我一个0%的CPU?我不是在申请之外寻找外部工具.以下是我的代码示例:
otherApp = new otherApplication.Application();
PerformanceCounter cpuCounter = new PerformanceCounter("Process",
"% Processor Time", "otherApplication");
//This next line is to check if PerformanceCounter object is working
//MessageBox.Show(cpuCounter.NextValue().ToString());
stopwatch.Start();
while (cpuCounter.NextValue() > 10)
{
}
stopwatch.Stop();
Run Code Online (Sandbox Code Playgroud)
编辑:更改代码以说出otherApp和otherApplication而不是myApp和myApplication,以便更容易理解.
| 归档时间: |
|
| 查看次数: |
2336 次 |
| 最近记录: |