13

Is there any way I can pause all UI Update commands in Winforms?

Or I have a slight feeling I'm trying to go about this the completely wrong way, so is there another way around my problem: I basically load a saved state of a control, which loads new controls all over it. However I do some of this in the UI thread, and some of the data loading from another thread, which then fills the UI.

So the effect I have when it is loading is that the user can see a few of the controls appearing in one place, then moving to another place on the form, changing values, etc.

I'd like to get a loading screen instead of this and load the controls in the background. It's quite a large application and its not THAT important so redesigning my code isn't really an option.

Can I simply stop all Update() commands on a control while a method is executing?

Connell
  • 13,925
  • 11
  • 59
  • 92
  • my suggestion - dont show form while its rendering. If you freeze UI user can see empty form and after a while boom all controls appears. – Renatas M. Oct 14 '11 at 10:00
  • That's fine, sorry I didn't explain this bit. The default form contains a blank version of this control and then the user can drag the controls around and select certain options, add new controls, etc, then save the control's state to XML. This question is for when the user decides to open an existing file. – Connell Oct 14 '11 at 10:08

2 Answers2

16

You can use the SuspendLayout and ResumeLayout methods to wrap the setup of UI in one operation (without the update of the rendering).

Basically (assuming SomeMethod is in the form class):

private void SomeMethod()
{
    this.SuspendLayout();
    // all UI setup
    this.ResumeLayout();
}
Steve B
  • 36,818
  • 21
  • 101
  • 174
  • 1
    Brilliant, I knew it'd be something simple, I was just Googling for the wrong stuff it seems. Thank you! – Connell Oct 14 '11 at 10:05
  • 1
    Unfortuntelly it doesn't work while Form is already visible. There is a better way: http://stackoverflow.com/questions/13711812/parallel-generation-of-ui/15020157#15020157 – Wojciech Kulik May 31 '14 at 23:41
4

it really depends on your form logic, in general you should not overload the Load or Show method with too much things so that the form can be shown and drawn quickly and always look responsive.

in some cases it could help to use the SuspendLayout and ResumeLayout methods, see here: Control.SuspendLayout Method

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • It's not on the load. The control loading is initially in the designer code, which loads a blank control, as such. But there is a LoadData method on the control which is loading the form state from an XML file after the user is prompted to open a file. – Connell Oct 14 '11 at 10:07