Justin,
The short answer, call the static DoEvents method on the Application
class in the System.Windows.Forms namespace.
The long answer has to do with how windows process messages sent to
them. When you click on your button, a windows message is sent to your
button class indicating that the button was pressed. .NET handles this
message and then transforms it into your event.
This is just one of a number of windows messages that can be sent to
your window. Many different messages are sent to tell it when it is
being moved, when it is clicked, the mouse is moving over it, etc, etc,
etc.
In order to handle all of this, all the messages for the entire
application are placed in a queue, and processed sequentially.
When you set the text of a label, you are setting the bit of data
indicating what the text is, but the label still has to repaint itself.
In order to do this, the label places a message into the queue saying
"hey, repaint myself".
However, when you are processing your button press, you can't process
other windows messages. You have to wait until your code is done to get
to the point where the label can process the message to repaint itself.
Now, calling DoEvents will process all the messages that are in the
queue. In your case, your label will update, but if other messages have
been waiting (and there most likely are, like mouse movements, window
deactivations, clicks, etc, etc), then they are processed as well.
This could introduce some subtle bugs (involving synchronization)
into your program.
The better solution would be to do all of your wonderful things on
another thread, and then call the Invoke method on a control, passing a
delegate to update the UI when needed. This will cause a message to be
sent to the UI thread to update itself, but since you are handling all
of the processing on the other thread, it should update immediately (or
very close to).
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
So another simple question from me
as im working away ive made a button that does a series of wonderful
things.. and as it goes along it suppose to update a label to let the
user know what stage its at... but.. the rendering isnt done until
after the code is completed... how do i tell it to render so that the
label gets updated?
Thanks
Justin