Quick Question on Application.Exit()

D

Doug Handler

In C# 2.0, w/ the Program.cs file, i noticed that the Application.Run(new
appname()) is run twice. I have in my constructor to do a check for a
settings file, and if it isn't found, the application exits. But the app
doesn't exit, can someone tell me what i'm doing wrong?

here's my code:
if (filename == null)

Application.Exit();
 
N

Nicholas Paldino [.NET/C# MVP]

Doug,

There is no reason why the static Run method on the Application class
should be run twice. That's a definite error. I would remove the one after
the initial call, and then see what happens.

Hope this helps.
 
D

Danny Tuppeny

Doug Handler said:
In C# 2.0, w/ the Program.cs file, i noticed that the Application.Run(new
appname()) is run twice. I have in my constructor to do a check for a
settings file, and if it isn't found, the application exits. But the app
doesn't exit, can someone tell me what i'm doing wrong?

here's my code:
if (filename == null)

Application.Exit();

Are you sure your logic makes sense? Above you say "if it isn't found", but
this code doesn't check for the existance of a file, only that the filename
variable isn't null. Are you meaning to do something like:

if (!File.Exists(filename))
{
Application.Exit();
}

?
 
P

pvdg42

Doug Handler said:
In C# 2.0, w/ the Program.cs file, i noticed that the Application.Run(new
appname()) is run twice. I have in my constructor to do a check for a
settings file, and if it isn't found, the application exits. But the app
doesn't exit, can someone tell me what i'm doing wrong?

here's my code:
if (filename == null)

Application.Exit();
How sure are you that filename is really null? I ask because a quick test
using a String explicitly set to null allows Application.Exit() to be called
and results in the expected termination here.
Have you walked through the code in the debugger to see the value assigned
to filename?
Note also that "" appears not to be equivalent to null. If I assign: String
s = ""; then testing if(s == null) returns false.
 
D

Doug Handler

Nicholas,

I didn't add the extra one. Create a simple winform project and go into the
Program.cs file and put a breakpoint. Then in the constructor of the main
form, (after the InitializeComponent() ) do something like call
messagebox.show. after that line put the Application.exit. See if it runs
"twice".

Doug
Nicholas Paldino said:
Doug,

There is no reason why the static Run method on the Application class
should be run twice. That's a definite error. I would remove the one
after the initial call, and then see what happens.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Doug Handler said:
In C# 2.0, w/ the Program.cs file, i noticed that the Application.Run(new
appname()) is run twice. I have in my constructor to do a check for a
settings file, and if it isn't found, the application exits. But the app
doesn't exit, can someone tell me what i'm doing wrong?

here's my code:
if (filename == null)

Application.Exit();
 
D

Doug Handler

Regarding my code below, i'm 100% positive its null. My issue isn't w/ the
code below, I know it executes fine. BTW, in a different class filename is
set to null if it isn't found for design purpose. My concern is that for
whatever reason, when i put the logic to exit the application, the
Application.Run statement in the Program.cs is executed twice. I know this
by the fact i put a breakpoint on it. Try it - see my previous posting.

I'm going to assume that you don't have an answer.

dh
 

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