Label text not updated

2020-03-26 11:48发布

I have a Windows Form with a status bar which shows the current state of application. I have a class named AppState with update the Label in the status bar and in dispose it changes the state back to "Ready".

In code when I do an operation like:

using (AppState state = new AppState("Processing..."))
{
     //Do some work that take some seconds
}

But the label remains the same. I am not getting any exceptions. The label text is updated but on UI it keeps on showing previous value. Am I missing anything here?

santosc you are right, thats the only thing I am doing. Here is the AppState code

public class AppState : IDisposable
{
    static string Default = "Ready";

    public AppState(string status)
    {
        Form.StatusLabel.Text = status;
    }

    public void Dispose()
    {
        Form.StatusLabel.Text = Default;
    }
}

标签: c#
6条回答
▲ chillily
2楼-- · 2020-03-26 12:14

Maybe multiple threads could solve your problem.

The easiest way is using a BackgroundWorker.

The reason is that the UI is only able to redraw when the UI thread has nothing else to do. And you are blocking it with your calculation.

查看更多
Ridiculous、
3楼-- · 2020-03-26 12:15

use Label.Refresh(); it saves a lot of time.This should work for u

查看更多
Fickle 薄情
4楼-- · 2020-03-26 12:19

Looks like you want to put Application.DoEvents() after setting the StatusLabel text field value. This tells Windows Forms to process the Windows event queue for your form, causing changes to be repainted.

查看更多
趁早两清
5楼-- · 2020-03-26 12:26

in order to be "thread safe" use Invoke, and test with the InvokeRequired in the form like:

    // code outside the myForm:-----------------------
    if (myForm.InvokeRequired)
        myForm.Invoke(new ChangeLabelEventHandler(ChangeLabel), "teeeest");
    else
        myForm.ChangeLabel("teeeest");


    // code in the myForm:-----------------------------
    public delegate void ChangeLabelEventHandler(string newText);

    private void ChangeLabel(string newLabelText)
    {
        this.label1.Text = newLabelText;
    }
查看更多
别忘想泡老子
6楼-- · 2020-03-26 12:32

It's always the same thing...

If you want to start something that takes a while, don't do it within your GUI thread or your GUI will freeze (no updates of label, no resizing, no moving, no whatever).

Filling your code on thousand places with Application.DoEvents() is also a bad practice.

If you have some long running task (long means > 1 sec) you should probably use a BackgroundWorker. Maybe it's a little bit harder at the beginning, but you will love it if your program gets more complex. Due to the fact, that this has already being discussed several time, here is a link with some sample code.

Now that you know the right tool (BackgroundWorker) to solve your problem, you should get it to work (or ask another question about your new specific problem).

查看更多
神经病院院长
7楼-- · 2020-03-26 12:41

I'm new to C# stuff, but why can't you just do something like:

private void updateStatusBar(string status)
{
    if (StatusLabel.InvokeRequired)
    {
        StatusLabel.Invoke((MethodInvoker)(() =>
                   {
                       StatusLabel.Text = status;
                   }));
    }
    else
    {
         StatusLabel.Text = status;
    }
}

When you want to update the status?

查看更多
登录 后发表回答