There is nothing wrong with what you are doing. At least not in the shown snippet. The configuration of Timer
can be wrong. Make sure you are right seeing the example in the documentation.
The timer used is correct for Windows Forms. There is no reason to use thread there. Not even asynchrony seems adequate. The function of the timer is just to call the established method. If it runs fast, and this type of task must perform very fast, otherwise it is unviable, everything will work almost without realizing. Any mechanism trying to use thread would only make the situation worse.
Maybe the whole form is locking the application. That’s another problem. It is actually the problem of what is running the most, because the form is sovereign and it needs to allow other parties to run freely. But I’m just speculating, since there are no details in the question.
I would only change the way it captures information. I would not use WMI. I would create a property in the form to maintain a Windows performance counter:
private PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
And then you would have:
private void timer1_Tick(object sender, EventArgs e) => label42.Text = cpuCounter.NextValue().ToString();
I put in the Github for future reference.
Maybe you need to give it a little adapted to what you need, but that’s the basis.
Although it can be improved, this should not block the program. How the
Timer
? What minimum time you need to update?– Maniero
Have you tried putting this into a thread? I’m not sure I understand your question, but how it hangs, I would try putting it into a thread. http://www.macoratti.net/10/09/c_thd1.htm
– pnet
He must be using the
Interval
like 1 second or less. @bigown– Jéf Bueno
Where is the implementation of the timer?
– Bacco
Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?
– Maniero