Delayed initialization of a System.Window.Forms.Form?
-
Hi! I'm creating a WinForm to display data from an XML Web Service. Typically in the WinForm constructor, after the call to the VS designer's InitializeComponent(), I initialize the controls of my form. Problem is, that takes quite a while and because the form is not yet visible when the constructor is called, the end-user feels like the app is not working. So instead, I'd like to delay my initialization till after the form has been fully displayed, but I have yet to find an event for that in the Form class. OnLoad explicitly says it is generated befor the form is displayed. OnActivate doesn't seem to go thru... As a last resort, I could have the form post itself an event, and hope the app gets it once the window is finished displaying. Any better non-hack solution? TIA R/
-
Hi! I'm creating a WinForm to display data from an XML Web Service. Typically in the WinForm constructor, after the call to the VS designer's InitializeComponent(), I initialize the controls of my form. Problem is, that takes quite a while and because the form is not yet visible when the constructor is called, the end-user feels like the app is not working. So instead, I'd like to delay my initialization till after the form has been fully displayed, but I have yet to find an event for that in the Form class. OnLoad explicitly says it is generated befor the form is displayed. OnActivate doesn't seem to go thru... As a last resort, I could have the form post itself an event, and hope the app gets it once the window is finished displaying. Any better non-hack solution? TIA R/
Have you tried putting in a "Show()" prior to the InitalizeComponent()? In a simple test it seems to work here in that I can set a breakpoint at InitalizeComponent and it has the window already shown.
public MyForm() { Show(); InitalizeComponent(); }
You might want to make sure to set the window size prior to showing it with the:ClientSize = new System.Drawing.Size(x,y);
Rocky Moore <>< -
Have you tried putting in a "Show()" prior to the InitalizeComponent()? In a simple test it seems to work here in that I can set a breakpoint at InitalizeComponent and it has the window already shown.
public MyForm() { Show(); InitalizeComponent(); }
You might want to make sure to set the window size prior to showing it with the:ClientSize = new System.Drawing.Size(x,y);
Rocky Moore <><But then won't the form show first without any controls on it? I wonder what the redrawing will do to performance.
Paul Watson wrote: "At the end of the day it is what you produce that counts, not how many doctorates you have on the wall." George Carlin wrote: "Don't sweat the petty things, and don't pet the sweaty things." Jörgen Sigvardsson wrote: If the physicists find a universal theory describing the laws of universe, I'm sure the asshole constant will be an integral part of that theory.
-
But then won't the form show first without any controls on it? I wonder what the redrawing will do to performance.
Paul Watson wrote: "At the end of the day it is what you produce that counts, not how many doctorates you have on the wall." George Carlin wrote: "Don't sweat the petty things, and don't pet the sweaty things." Jörgen Sigvardsson wrote: If the physicists find a universal theory describing the laws of universe, I'm sure the asshole constant will be an integral part of that theory.
I think so too. I ended up placing the initialization call in the Activated event of the form. Experience shows that the form starts displaying, some items are partially visible and some are not (sounds like the message pump not having time to finish redrawing everything). It's not ideal but it's a better user experience -- the classical Windows Application not refreshing because of some heavy computation is of course not the desirable effect but something end-users can understand :-) R/
-
I think so too. I ended up placing the initialization call in the Activated event of the form. Experience shows that the form starts displaying, some items are partially visible and some are not (sounds like the message pump not having time to finish redrawing everything). It's not ideal but it's a better user experience -- the classical Windows Application not refreshing because of some heavy computation is of course not the desirable effect but something end-users can understand :-) R/
I misunderstood what you were asking for. I thought it was the fact that the form did not appear at all on the screen so they saw nothing happening. To have your form come up and display prior to your own initalization code, try this in your constructor:
public MyForm() { InitializeComponent(); Show(); foreach(Control control in this.Controls) control.Refresh(); // Your initialization goes here }
Rocky Moore <>< -
I misunderstood what you were asking for. I thought it was the fact that the form did not appear at all on the screen so they saw nothing happening. To have your form come up and display prior to your own initalization code, try this in your constructor:
public MyForm() { InitializeComponent(); Show(); foreach(Control control in this.Controls) control.Refresh(); // Your initialization goes here }
Rocky Moore <><Interesting approach but 2 questions come to mind: - Isn't Show() suppose to display the dialog modeless? What's the impact of the caller using ShowModal() later on? - I naively assumed this.Refresh() would do the equivalent of the foreach(Controls)...Refresh() loop, doesn't it? Definitely worth trying anyway, thanks! R/
-
Interesting approach but 2 questions come to mind: - Isn't Show() suppose to display the dialog modeless? What's the impact of the caller using ShowModal() later on? - I naively assumed this.Refresh() would do the equivalent of the foreach(Controls)...Refresh() loop, doesn't it? Definitely worth trying anyway, thanks! R/
Yes, if you are using ShowDialog, before you call that you would have to either call Hide() or visible=false which will cause a little flicker. I did not bother trying the member refresh since I thought it might delay the refresh of the controls but it does the same. If you initialization code is very long you could also just have one control that you refresh (only one that would show) that would have a message like "one moment please", which would inform your using that something is happening so that even if they do see your form they will not try to access it until your initalization is over. Once your initailization is over, you can hide that one control and do your Hide and ShowDialog. Rocky Moore <><
-
But then won't the form show first without any controls on it? I wonder what the redrawing will do to performance.
Paul Watson wrote: "At the end of the day it is what you produce that counts, not how many doctorates you have on the wall." George Carlin wrote: "Don't sweat the petty things, and don't pet the sweaty things." Jörgen Sigvardsson wrote: If the physicists find a universal theory describing the laws of universe, I'm sure the asshole constant will be an integral part of that theory.
Use CreateHandle() instead of Show(), but is still feels like a hack to me too. -- -Blake (com/bcdev/blake)