4

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?

derekantrican
  • 1,891
  • 3
  • 27
  • 57

5 Answers5

7

As P. Kouvarakis said, the solution was this:

toolStripStatusLabel1.Text = "Test";
statusStrip1.Update();
derekantrican
  • 1,891
  • 3
  • 27
  • 57
0

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.

C.M.
  • 1,474
  • 13
  • 16
0
  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"

AEM
  • 1,354
  • 8
  • 20
  • 30
-1
toolStripStatusLabel1.Text = "Test";
Application.DoEvents();
-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();
}
ejderuby
  • 710
  • 5
  • 21
HRolle
  • 1
  • DoEvents has potential side affects. You should avoid using it in most circumstances. – LarsTech Aug 13 '19 at 16:42
  • DoEvents() does indeed have strange and possibly serious side effects. I'm fighting DoEvents() right now where the application freezes because of liberal usage of DoEvents(). Unrolling the troubles will be difficult, it's been a week and I'm just now getting close to the issue. – John Dyer Feb 26 '21 at 18:43