constructing a form before calling Application.Run

  • Thread starter Thread starter Lee Crabtree
  • Start date Start date
L

Lee Crabtree

I remember when I was first getting into .NET Forms programming that
there was a rather emphatic rule about not constructing a form before
calling Application.Run with it. So this:

Application.Run(new Form1());

was okay, but this:

Form1 form = new Form1();

Application.Run(form);

Was a Bad Idea(TM). Indeed, even now, in VS2005, when a Forms app is
being generated, Studio creates the Main method in the first style.

The problem is that I need to make a couple of things in the form
visible to my global exception handler, so that I can quickly react to
an exception and put some hardware in a safe state. Is it still
dangerous to construct a form before calling Application.Run?

Lee Crabtree
 
Lee,
I must be ReallyReallyDumb, but frankly, I don't understand what the big
difference is between the first and the second examples. At the least, not
how it could be "dangerous".
Peter
 
Okay, maybe it isn't. It could just be that the sources from which I
learned were overly cautious. I can't really come up with a good reason
for why it would ever be dangerous. I just remember seeing a couple of
articles stating that you shouldn't create an object before calling
Application.Run.

Lee Crabtree
 
Peter,

I'm with you on that. I must've missed that day in WinForms Writing
101.

I've modified Main() to instantiate the form in an instruction previous
to Application.Run many times, never with ill effect.
From the perspective of what gets run, is there even a difference
between the two patterns? I think they both do exactly the same thing,
in the same order. Anyone know any difference? This could be the new
thing I learn today.



Stephan : IReallyReallyDumb
 
Nope I've been doing that for ages.
If it was dangerous then Application.Run would surely take Type and object[]
as args and construct it itself?
 
Hi Stephan,

The difference is that the second example uses a local variable, which means a
couple of extra CIL instructions that use the stack, but nothing to worry
about :)

Perhaps Lee remembered reading that you shouldn't show a Form before calling
Application.Run, but I don't think there is anything wrong with that either.
For instance, I've showed splash screens like that before.
 
Lee Crabtree said:
Okay, maybe it isn't. It could just be that the sources from which I
learned were overly cautious. I can't really come up with a good reason
for why it would ever be dangerous. I just remember seeing a couple of
articles stating that you shouldn't create an object before calling
Application.Run.

Sounds like an old wives' tale to me. After all, calling

Application.Run (new Form1())

*is* creating the object before calling Application.Run.
 
You're a wily one, you are.

Judging from the wealth of responses, I'd say that my initial
assumptions were definitely flawed. I never thought I'd be glad I was
wrong.

Thanks, everybody.

Lee Crabtree
 

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

Back
Top