By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,444 Members | 3,182 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,444 IT Pros & Developers. It's quick & easy.

constructing a form before calling Application.Run

P: n/a
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
Oct 25 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Lee Crabtree" wrote:
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
Oct 25 '06 #2

P: n/a
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 Bromberg [C# MVP] wrote:
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
Oct 25 '06 #3

P: n/a
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

Peter Bromberg [ C# MVP ] wrote:
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Lee Crabtree" wrote:
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
Oct 25 '06 #4

P: n/a
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?

"Lee Crabtree" <lc*******@goisi.comwrote in message
news:45**************@goisi.com...
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 Bromberg [C# MVP] wrote:
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

Oct 25 '06 #5

P: n/a
Hi Stephan,
>>>Application.Run(new Form1());
vs
>>Form1 form = new Form1();
Application.Run(form);
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.

--
Dave Sexton

"ssamuel" <ss*****@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
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

Peter Bromberg [ C# MVP ] wrote:
>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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Lee Crabtree" wrote:
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

Oct 25 '06 #6

P: n/a
Lee Crabtree <lc*******@goisi.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 25 '06 #7

P: n/a
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

Jon Skeet [C# MVP] wrote:
Lee Crabtree <lc*******@goisi.comwrote:
>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.
Oct 25 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.