开发者

Label text not updated

开发者 https://www.devze.com 2022-12-14 02:17 出处:网络
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

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 开发者_如何学PythonDispose()
    {
        Form.StatusLabel.Text = Default;
    }
}


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).


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.


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;
    }


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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消