Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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):
C#
foreach (ManagementObject search in win32proc.Get())
                    currclock = Convert.ToDouble(search["CurrentClockSpeed"]) / 1000.0; //Current Clock (GHz) 


It works fine, clock speed is updated 1 time/sec. But it makes my application seem like "freeze"; I can't click any button or close application.
Hope anyone can help me solve this problem or show me others way to get CPU's current clock speed in real-time.

Thanks anyway!

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

Solution:
Create a background worker thread to get current clock speed, it works independently with UI thread.
For anyone who wants to know about Background Worker: BackgroundWorker Threads and Supporting Cancel[^]
Posted
Updated 3-Mar-18 11:20am
v4
Comments
[no name] 6-Jul-13 10:30am    
As Windows is not a real time operating system, the best you can do is get this information when Windows allows you to. If your app is "freezing" then use a background thread.

1 solution

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.
 
Share this answer
 
Comments
Lê Ngọc Quý 6-Jul-13 22:18pm    
Thank you very much Ron Beyer. I've solved this problem!
BillWoodruff 3-Mar-18 20:42pm    
+5 a good solid answer that does not deserve a down-vote.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900