[C#]实时获取处理器的当前时钟速度 [英] [C#] Get current clock speed of processor in real-time

查看:498
本文介绍了[C#]实时获取处理器的当前时钟速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位大家好!



我的项目有问题。希望有人能帮助我!非常感谢。

我想实时获取处理器的当前时钟(每秒更新一次)以绘制图形。

我创建一个1000毫秒的定时器在Timer_Tick()函数内部,我使用此代码获取处理器的当前时钟速度(GHz):

Hello everybody!

I have a problem in my projects. Hope anyone can help me! Thank you very much.
I want to get current clock of processor in real-time (update each second) to plot a graph.
I create a Timer with 1000 miliseconds interval, inside the Timer_Tick() function, I use this code to get current clock speed of processor (GHz):

foreach (ManagementObject search in win32proc.Get())
                    currclock = Convert.ToDouble(search["CurrentClockSpeed"]) / 1000.0; //Current Clock (GHz) 





工作正常,时钟速度更新1次/秒。但它使我的应用程序看起来像冻结;我无法点击任何按钮或关闭应用程序。

希望任何人都可以帮我解决这个问题,或者告诉我其他人实时获取CPU当前时钟速度的方法。



无论如何,谢谢!



--------------------



解决方案:

创建后台工作线程以获取当前时钟速度,它与UI线程独立工作。

对于任何想要了解后台工作者的人: BackgroundWorker线程和支持取消 [ ^ ]

推荐答案

我希望您在try ... catch块中拥有该代码。您可能会抛出大量异常,导致程序在例程中锁定,同时您尝试在所有内容上获取无效对象,直到您获得有效的对象为止。



你需要做的是找到正确的管理对象并保存它,然后只需获取计时器中的值。循环遍历所有管理对象并尝试将它们全部转换可能会导致问题。例外是昂贵的,所以尽你所能避免它们。
I hope you have that code in a try...catch block. You are probably throwing a lot of exceptions which causes the program to lock up in the routine while you are attempting to get an invalid object on everything until you get the one that works.

What you need to do is find the correct management object and save it, then just get the value in the timer. Looping through all the management objects and trying to convert them all is probably causing issues. Exceptions are expensive, so do everything you can to avoid them.


这篇关于[C#]实时获取处理器的当前时钟速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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