Form Load method

J

Julie

My application has a couple of modal forms. The first gets constructed
in Main(), followed by an Application.Run(_form);

Traditionally I have had a "Start" button on that form which when
pressed would execute start(), which would take the values in the form
and start a new thread which would construct the second form.

Now I would like to include an optional command line argument which
would execute start() without the user needing to press the "Start"
button.

So, I added my start() function as a new thread that gets launched
from my Load callback for my first form. However, when I run in Debug
mode, my first form doesn't end up getting displayed at all for some
reason. Is it a bad idea to launch a new thread from the Load
callback? If I run by "Start without Debugging", the first form comes
up fine.

Any ideas? Thanks!
 
J

Julie

First thing: if you don't post a proper concise-but-complete code example
that reliably demonstrates the problem, no one can say for sure how or why
your code is broken.

Beyond that, the only advice I can offer is to mind your P's and Q's.  For
any Form instance to work property, it has to be created in an STA thread,
and there has to be a message pump running in the same thread where the
instance was created.  The normal way for the message pump to run is for
the Application.Run() method to be called; it handles the message pump and
won't return until it's time to exit the application (or at least that
thread of the application).

If you don't call Application.Run(), your form won't get any messages,
including the one that tells it to draw itself and of course all the
messages related to user input.

Maybe that's what's wrong with your program.  Maybe it's not.  Who can
tell?  You didn't post any code.

Pete

I apologize. One reason I didn't post is because my software is
classified, and I would have to go through a declassification process
in order to copy and paste it here. But that doesn't mean I can't
manually type code fragments at least.

I guess I was just trying to ask if it was inappropriate to launch a
thread from a Form Load callback. I guess it's not. Perhaps that was a
strange question to ask.

I think I figured out my problem (which yes, would have been easier to
identify if all of my code had been pasted on here). The form's
WindowState gets set to Minimized in the Load callback. For some
reason, when debugging, this causes the window to go away completely
once the second thread gets launched, such that you can't see it in
the Windows task bar at the bottom of the screen. (Although, the
thread is still sitting on the Application.Run() line) Oh well. I
don't actually need the WindowState to get set to Minimized, so I can
just take that out.
 
J

Julie

First thing: if you don't post a proper concise-but-complete code example
that reliably demonstrates the problem, no one can say for sure how or why
your code is broken.

Beyond that, the only advice I can offer is to mind your P's and Q's.  For
any Form instance to work property, it has to be created in an STA thread,
and there has to be a message pump running in the same thread where the
instance was created.  The normal way for the message pump to run is for
the Application.Run() method to be called; it handles the message pump and
won't return until it's time to exit the application (or at least that
thread of the application).

If you don't call Application.Run(), your form won't get any messages,
including the one that tells it to draw itself and of course all the
messages related to user input.

Maybe that's what's wrong with your program.  Maybe it's not.  Who can
tell?  You didn't post any code.

Pete

I apologize. One reason I didn't post is because my software is
classified, and I would have to go through a declassification process
in order to copy and paste it here.
Of course, I could still manually type in a little bit of the code
without typing in all of it.

I was just trying to ask if it for some reason was a bad idea to
launch a thread from a Form Load callback. Maybe that's a silly
question. I guess there isn't anything wrong with that.

I did figure out the problem. The Load callback was minimizing the
form. For some reason, then launching a thread following this
minimization caused the form to disappear completely (even though I
could see the thread waiting at the line: Application.Run(_form); Once
again, this disappearance was only happening while debugging.
 
J

Julie

Actually, what it doesn't mean is that you can't create a proper,
standalone example that demonstrates the issue.  Even for
non-classified/non-proprietary code, the worst thing one can do is just
copy and paste their whole code as the example.

You need to create from scratch a simple example that shows exactly the
problem, and nothing more.


You can start a thread from anywhere, including the Form.Load event
handler.  That's not a problem per se.  However, the way you use that
thread could be.

Also, please stop double-posting.  Once for each message is plenty.  :)

Pete

Got it.

The reason for my double reply was because for 20 minutes my reply was
not visible. At some point, I had to decide that due to some error my
reply would never appear, and thus I would need to re-post.
(especially because I thought the reply was an automatic thing, as
opposed to some forum monitor-controlled thing) I suppose 20 minutes
was too early to make this decision.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top