3

I have a Windows form that I'd like to be able to refresh with a status of the work that's going on in the background in different threads.

Problem is, even if I change the label on the form, it doesn't immediately refresh; it seems that the work happening on the other worker threads is preventing the screen from updating.

How do I force the form to refresh the new value of the status label immediately?

Shaul Behr
  • 36,951
  • 69
  • 249
  • 387

2 Answers2

9

Shortest path:

label1.Text = "....";
label1.Update();
H H
  • 263,252
  • 30
  • 330
  • 514
  • 2
    I just slapped my forehead and shouted "DOH!" Answer credit for the shortest path... :) – Shaul Behr Jan 27 '10 at 09:34
  • If the screen isn't updating because of work on the worker threads, then updating the Label won't do anything, because the screen isn't updating.... Will it ? – Nick Haslam Jan 27 '10 at 09:36
  • Nick, just try it. See http://stackoverflow.com/questions/952906/how-do-i-call-paint-event/952964#952964 – H H Jan 27 '10 at 09:41
  • 1
    @Old Nick: IIRC Control.Update refreshes the Control immediately, bypassing the message queue. So yes, it should work in a busy thread, too. – Niki Jan 27 '10 at 09:45
1

I asked a similar question a few days ago.

The best option, that I'm using now, is a separate thread running a status form.

This is the suggested solution, that I received, which works by displaying a form, which then updates on a 'tick', and ensures that regardless of whether the UI on your app is updated or not, the status form is updated.

Cheers

Community
  • 1
  • 1
Nick Haslam
  • 1,512
  • 1
  • 24
  • 47