Alex Rivera | Logout

Force GUI update from UI Thread

Asked 2009-09-01T07:00:57.493
84

In WinForms, how do I force an immediate UI update from UI thread?

What I'm doing is roughly:

label.Text = "Please Wait..."
try 
{
    SomewhatLongRunningOperation(); 
}
catch(Exception e)
{
    label.Text = "Error: " + e.Message;
    return;
}
label.Text = "Success!";

Label text does not get set to "Please Wait..." before the operation.

I solved this using another thread for the operation, but it gets hairy and I'd like to simplify the code.

Edit
Report

1 Answer

18

Call Application.DoEvents() after setting the label, but you should do all the work in a separate thread instead, so the user may close the window.

answered 2009-09-01T07:07:29.807

Your Answer