473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

constructing a form before calling Application.Run

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
7 2915
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Programatix | last post by:
Hi, I'm working on a project which includes XML WebServices and Windows Form application. The Windows Form application will call the XML WebServices to retrieve data from database. The data...
5
by: RAJ | last post by:
hi plz tell me how to know "how window is going to close"... i have to right code for X button of forms... plz telll me thanks bye
3
by: Brian Keating EI9FXB | last post by:
Hello again, I've already placed a few posts on this topic. This time i've a simple application that exhibits my problem, I've placed sample solution 8k on my website should anyone be interested...
2
by: Renzo | last post by:
hi ! i've recently begun developing in c# and i'd like to know what's the best way of calling a form from an already instanced form ? I have done a "Login" form which I run in the Main() using...
10
by: Partha Protim Roy | last post by:
Hello, My problem is: I got a Login form, so once the user enter vaild Username & Password another form opens. How do I close the login form?
5
by: Miro | last post by:
I will try my best to ask this question correctly. I think in the end the code will make more sence of what I am trying to accomplish. I am just not sure of what to search for on the net. I...
0
by: jonpc2001 | last post by:
I'm trying to workout if it is possible to startup a windows form application and create multiple tab pages (with various controls) using a different thread from the main one. The reason why I...
3
by: bsturg21 | last post by:
Hello, I have a windows form that has a series of linklabels on it, and I need to have each linklabel, when clicked, open a separate windows form that has a single paramter passed into it. The...
7
by: =?Utf-8?B?TWF0dA==?= | last post by:
Hi I have an app that runs without a main form, just a notification icon, when the user clicks the icon the form is shown, and when the form is minimized it's hidden. This all works great,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.