tags:

views:

1547

answers:

5

I have a form with buttons, textBoxes and userControls on it. When a button is clicked, it calls a method in another class. In this class a Message box opens. When user clicks OK, the messageBox closes, and the class method proceeds for 10 or so seconds before ending. During this 10 seconds, what ever textBox, or button the message box was over still displays the messageBox (They are not getting repainted).

The question is how can I force everything to repaint on the form. The problem is the class that has the messageBox does not have any knowledge of the form that called it

Frank

A: 

To force everything to repaint you can call Invalidate() on the main form.

scottm
+5  A: 

Is that 10 seconds spent doing work entirely in the UI? If not, it should really be done on a separate thread. Even if you refresh the form itself, you're still going to have a unresponsive UI for 10 seconds, which isn't ideal.

See my threading tutorial for an example of executing code in a different thread and calling back to the UI thread. Be aware that this is the "old" way of doing things - BackgroundWorker makes things somewhat simpler.

Jon Skeet
I agree that you should launch it on a different thread.
Vincent
+3  A: 

You can try with

Application.DoEvents()

right after the message box closes. However - unless you execute the method that you are calling in that other class in a background thread - your UI will not be responsive for 10 seconds.

0xA3
Thanks - that helped me.
frou
A: 

Your problem is that you are doing your work on the UI thread. The UI will not get repainted until your method returns which will allow the Windows message loop to continue.

The solution is to run your work method on a different thread. Perhaps the BackgroundWorker class will solve your problem nicely.

Edit: See this article for an in depth explanation:
http://www.yoda.arachsys.com/csharp/threads/winforms.shtml

driis
LOL - we were both editing to refer to the same article :)
Jon Skeet
+3  A: 

The problem here is that you have processing that is occurring on the UI thread and blocking the paint message. Calling Refresh or Invalidate isn't going to fix this, as you are still blocking on the thread that would perform those operations.

Rather, you should take that processing and move it to another thread, and then update your UI thread (appropriately, through the Invoke method, more likely than not) as you perform the work on the background thread.

casperOne