Update label without crashing program

Asked

Viewed 995 times

4

I need to update my label at all times when I use the timer it updates, but it crashes the program

Program.Cs

public static String Uso()
{

    ManagementClass mc = new ManagementClass("win32_processor");
    ManagementObjectCollection moc = mc.GetInstances();
    String Id = String.Empty;
    foreach (ManagementObject mo in moc)
    {
        Id = mo.Properties["LoadPercentage"].Value.ToString();
        break;
    }

    return Id;
}

Form1.Cs

private void timer1_Tick(object sender, EventArgs e)
{
    label42.Text = Program.HardwareInfo.Uso();
}

I can’t find a way that it updates the processor usage information and leaves my program running normally.

  • 1

    Although it can be improved, this should not block the program. How the Timer? What minimum time you need to update?

  • 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

  • 1

    He must be using the Interval like 1 second or less. @bigown

  • 1

    Where is the implementation of the timer?

  • 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?

2 answers

3

This must be happening because the timer interval (timer.Interval) is very short and the thread main gets locked always running the timer event.

You can do this in a thread by just changing the timer you’re wearing (System.Windows.Forms.Timer) by a System.Timers.Timer.

This should work for your case. Note that the event is now the Elapsed and no longer the Tick

public timer_Elapsed(Object source, ElapsedEventArgs e){
    this.Invoke(new MethodInvoker(() => label42.Text = Program.HardwareInfo.Uso()));
}

There are other things I can improve on, maybe even use a timer. But that should be enough to solve your problem without tampering with the code too much.

3

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.