Newbie Question: Doing tasks after form displays 'on load'

  • Thread starter Thread starter Damien Sawyer
  • Start date Start date
D

Damien Sawyer

Hi all,

I wish to do certain tasks on startup (load from databases etc). The problem
is, if I put these events in the Load method of the form, the form is not
displayed until they are finished.

Surely I don't need to do those tasks on another thread. Is there an event
that fires 'after' the form is displayed?

Can someone please point me in the right direction?

Thanks very much in advance,



Damien Sawyer
 
No, you need to do them on another thread, if you don't like this
behaviour.

If you do them in the UI thread after the form is loaded, then you will
have a form on your screen that is "frozen" and won't repaint itself
until after the tasks are done. That's what a single thread means: you
can do only one thing at a time (and handling screen painting is
already "one thing").

If you want the form up, and reactive, and at the same time be reading
from a database or doing other setup, then you need to do it in a
background thread.
 
Thanks very much Bruce.

Do you (or anyone else) know if there is there an event that fires (on the
main thread) AFTER the form has initally drawn itself? I would be content to
have the form frozen whilst the data is collected. At the moment, using
OnLoad to load my data, it is frozen before anything is displayed - ie (in
an SDI) the user double- clicks the exe file and then waits (with 'nothing'
happening) for 10 seconds for the data to load and the form to paint.

I hope that that all made sense.

Thanks in advance,



Damien Sawyer
 
Do you (or anyone else) know if there is there an event that fires (on the
main thread) AFTER the form has initally drawn itself? I would be content to
have the form frozen whilst the data is collected. At the moment, using
OnLoad to load my data, it is frozen before anything is displayed - ie (in
an SDI) the user double- clicks the exe file and then waits (with 'nothing'
happening) for 10 seconds for the data to load and the form to paint.
You want to start doing stuff after the window has correctly painted without
using a thread.
In C# I have no clue yet how to do it, but in the MFC way, there existed a
"idle" windows message that I used to trigger execution after the paint. So
maybe someone here can find out how to do that in C#?
 
Found it :-)

Stupidly simple too.
Create your function to work like this.

private void Application_Idle(object sender, EventArgs e) {
// put your code here
}

And initialize this, maybe in your Form_Load()
Application.Idle+=new EventHandler(Application_Idle);

That's all. :-)
 
Perhaps a better solution (employed by many other apps) is to display a
"splash screen" while your app is loading. Ferret (or Google :) around
for "splash screen" and you'll find several examples.

You can even use a background thread for your initialization
operations, so that your splash screen gracefully repaints itself,
secure in the knowledge that your user can't press any buttons or do
anything to your form before you're ready, becuase you haven't
displayed it yet.
 
What I've done in the past in Delphi, haven't figured out how as of yet in
C#, is to post a user message to the form from the load event. Then I would
respond to the user message to do my start up code. This would allow the
form to display correctly the first time, and would also ensure that the
message was only processed once.


--
Thanks
Wayne Sepega
Jacksonville, Fl

Enterprise Library Configuration Console Module Generator
http://workspaces.gotdotnet.com/elccmg

"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein
 
Thanks very much everyone.

The application.idle event looks like a great quick fix. I'll probably
retro-fit it to some existing applications.

I think for the future, the idea of a splash screen loading stuff on a
separate thread should be pretty workable.

Thanks again :-)



DS
 
Back
Top