tags:

views:

69

answers:

3

I have a user control that takes a several seconds to load. Is there a way to preload the control while the form is being loaded?

A: 

Do you have the source for the UserControl? If so, I would start by trying to optimize the UserControl. Sounds like the UC is creating too much UI objects on startup. Preloading will not save you anytime. The Winforms UI is single-threaded so that several seconds of load time would just be moved to the startup of the application. I don't know if you are creating multiple instances of the UC. If so, you might be able to reuse a single instance to avoid the creation expense.

Steve
I do have access to the source, and am only using 1 instance of it. To be honest, moving the load time of the control to app start-time is an acceptable answer. I would then use a splash screen, such as "Loading components" or something similar, as nobugz suggested.
Dave D
A: 

First, use a profiler to determine what is actually causing the slowdown.

Then, if it's appropriate, you can try to either optimize the code to make it faster (to an acceptable level), or refactor the independent code (such as data access) into a separate spot so that it can be executed asynchronously with the user control loading. That's about all you can do since the user control must be loaded on the same thread as the UI.

Jon Seigel
There is no data access to this control at the this stage. It is just several labels and a textbox that have empty Text fields until the user presses a 'populate' button. I am going to see what is slowing it down, but must be a bit later as I have a deadline to hit and must finish the 'features.'
Dave D
A: 

Instead of doing a bunch of work in the UserControl's constructor or Load event, do it in a worker thread. BackgroundWorker is good for that. This will give you quick startup of the form, but not necessarily quick availability of the user interface. Consider a splash screen.

Hans Passant
I like the splash screen idea. Availability is more acceptable than a bit slower application loading time. Thanks for the splash screen idea, I had never thought of it.
Dave D