derekantrican
derekantrican

Reputation: 2293

Updating toolStripStatusLabel immediately

I have a WinForms program that has a button, a statusStrip, and a toolStripStatusLabel in that statusStrip. If I click the button and this runs:

private void button1_Click(object sender, EventArgs e)
{
    toolStripStatusLabel1.Text = "Test";

    System.Threading.Thread.Sleep(5000);
}

Then the toolStripStatusLabel's text doesn't update until after the thread is done sleeping. How do I get it to update immediately, then sleep the thread?

Upvotes: 5

Views: 11541

Answers (5)

Vano Sz
Vano Sz

Reputation: 1

  1. You need to add the StatusStrip text label (as a child) ('cause you can't manipulate with the text of a parent component) like this:

child component

  1. When you double-click on statusstrip component you will see the next

2ble click pic

Do NOT add the text change code here, cause it will be refreshed only after a click on it (as you can see on the picture - statusStrip1_ItemClicked) so, any statusStrip1.Update(); this.Update(); etc can't help you.

  1. How to do it quickly. You can add the if where the target condition started like in this picture

example "if"

Upvotes: 0

C.M.
C.M.

Reputation: 1561

My status label change wasn't showing at runtime when the status strip had Spring set to true. I set Spring to false and it began showing.

Upvotes: 0

HRolle
HRolle

Reputation: 1

Last solution with Application.DoEvents() is the best. Application.DoEvents() must be appended to each StatusStrip change:

private void SetParseStatus(object sender, ParseStatusChangedEventArgs e)
{
    toolStripProgressBar1.Value = e.Percent;
    Application.DoEvents();
}

Upvotes: -1

derekantrican
derekantrican

Reputation: 2293

As P. Kouvarakis said, the solution was this:

toolStripStatusLabel1.Text = "Test";
statusStrip1.Update();

Upvotes: 8

Yiannis Leoussis
Yiannis Leoussis

Reputation: 549

toolStripStatusLabel1.Text = "Test";
Application.DoEvents();

Upvotes: -1

Related Questions