Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I freeze my UI rendering while my form loads?

Tags:

c#

winforms

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?

like image 989
Connell Avatar asked Oct 14 '11 09:10

Connell


2 Answers

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();
}
like image 164
Steve B Avatar answered Nov 12 '22 23:11

Steve B


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

like image 4
Davide Piras Avatar answered Nov 12 '22 21:11

Davide Piras