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

How to set exit code of non-console app?

P: n/a
I have a Windows application written in C# that I want to return a non-zero
exit code when it it run. The problem is that as a Windows application,
there doesn't seem to be a way to control this. The value returned by the
Main function has no impact on the value returned by the application.
Windows applications exit immediately, leaving their windows still open. Is
there a way to change the value a Windows application exits with. Basically
I want it to return 1 instead of 0. I need to do this because the
application is launched by another application that checked for the exit
code and this particular application expects that the program it launches to
return a non-zero exit code.
Nov 16 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Try setting Environment.ExitCode

Thanks,
Michael C., MCDBA

"Peter Steele" <ps*****@z-force.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I have a Windows application written in C# that I want to return a non-zero exit code when it it run. The problem is that as a Windows application,
there doesn't seem to be a way to control this. The value returned by the
Main function has no impact on the value returned by the application.
Windows applications exit immediately, leaving their windows still open. Is there a way to change the value a Windows application exits with. Basically I want it to return 1 instead of 0. I need to do this because the
application is launched by another application that checked for the exit
code and this particular application expects that the program it launches to return a non-zero exit code.

Nov 16 '05 #2

P: n/a
I'd already found this but it doesn't seem to have any effect. I added the
line

Environment.ExitCode = 1;

in Main but the application still exited with 0. I tried putting it in the
app's main constructor and this still didn't have any effect. If I compile
the app as a console application this works but not as a Windows
application.

So where am I supposed to set this ExitCode property?

"Michael C" <mi******@nospam.org> wrote in message
news:da**********************@news4.srv.hcvlny.cv. net...
Try setting Environment.ExitCode

Thanks,
Michael C., MCDBA

"Peter Steele" <ps*****@z-force.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I have a Windows application written in C# that I want to return a

non-zero
exit code when it it run. The problem is that as a Windows application,
there doesn't seem to be a way to control this. The value returned by the
Main function has no impact on the value returned by the application.
Windows applications exit immediately, leaving their windows still open.

Is
there a way to change the value a Windows application exits with.

Basically
I want it to return 1 instead of 0. I need to do this because the
application is launched by another application that checked for the exit
code and this particular application expects that the program it launches

to
return a non-zero exit code.


Nov 16 '05 #3

P: n/a
Try putting that line in the .Closing event of your form.

Thanks,
Michael C., MCDBA

"Peter Steele" <ps*****@z-force.com> wrote in message
news:Oe***************@TK2MSFTNGP11.phx.gbl...
I'd already found this but it doesn't seem to have any effect. I added the
line

Environment.ExitCode = 1;

in Main but the application still exited with 0. I tried putting it in the app's main constructor and this still didn't have any effect. If I compile
the app as a console application this works but not as a Windows
application.

So where am I supposed to set this ExitCode property?

"Michael C" <mi******@nospam.org> wrote in message
news:da**********************@news4.srv.hcvlny.cv. net...
Try setting Environment.ExitCode

Thanks,
Michael C., MCDBA

"Peter Steele" <ps*****@z-force.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I have a Windows application written in C# that I want to return a

non-zero
exit code when it it run. The problem is that as a Windows application,
there doesn't seem to be a way to control this. The value returned by the Main function has no impact on the value returned by the application.
Windows applications exit immediately, leaving their windows still open.
Is
there a way to change the value a Windows application exits with.

Basically
I want it to return 1 instead of 0. I need to do this because the
application is launched by another application that checked for the
exit code and this particular application expects that the program it

launches to
return a non-zero exit code.



Nov 16 '05 #4

P: n/a
You could do Environment.Exit(exitcode) but this method isn't recommended
for non console apps because it doesn' run the winforms closing/exit
methods/events properly.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"Peter Steele" <ps*****@z-force.com> schrieb im Newsbeitrag
news:#b**************@TK2MSFTNGP09.phx.gbl...
I have a Windows application written in C# that I want to return a non-zero exit code when it it run. The problem is that as a Windows application,
there doesn't seem to be a way to control this. The value returned by the
Main function has no impact on the value returned by the application.
Windows applications exit immediately, leaving their windows still open. Is there a way to change the value a Windows application exits with. Basically I want it to return 1 instead of 0. I need to do this because the
application is launched by another application that checked for the exit
code and this particular application expects that the program it launches to return a non-zero exit code.

Nov 16 '05 #5

P: n/a
I've tried calling Environment.Exit(exitcode) but the app still returns an
exit code of 0, regardless of what I specify in the Exit call.

Peter

"cody" <no****************@gmx.net> wrote in message
news:uq**************@TK2MSFTNGP09.phx.gbl...
You could do Environment.Exit(exitcode) but this method isn't recommended
for non console apps because it doesn' run the winforms closing/exit
methods/events properly.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"Peter Steele" <ps*****@z-force.com> schrieb im Newsbeitrag
news:#b**************@TK2MSFTNGP09.phx.gbl...
I have a Windows application written in C# that I want to return a

non-zero
exit code when it it run. The problem is that as a Windows application,
there doesn't seem to be a way to control this. The value returned by the
Main function has no impact on the value returned by the application.
Windows applications exit immediately, leaving their windows still open.

Is
there a way to change the value a Windows application exits with.

Basically
I want it to return 1 instead of 0. I need to do this because the
application is launched by another application that checked for the exit
code and this particular application expects that the program it launches

to
return a non-zero exit code.


Nov 16 '05 #6

P: n/a
As I understand it the result of Main() is the application's exit code. If I
use Visual Studio to create a C# Windows Application and then modify Main()
to be:
[STAThread]
static int Main() {
Application.Run(new Form1());
return 1;
}
it seems to exit with an exit code of 1. Maybe I'm missing something because
I don't know what you mean by "Windows applications exit immediately,
leaving their windows still open." Surely if there are still windows open
then the application can't have exited (unless the windows are created by a
different thread - I don't know what happens in that case when the initial
thread exits).

Chris Jobson

"Peter Steele" <ps*****@z-force.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I have a Windows application written in C# that I want to return a non-zero
exit code when it it run. The problem is that as a Windows application,
there doesn't seem to be a way to control this. The value returned by the
Main function has no impact on the value returned by the application.
Windows applications exit immediately, leaving their windows still open. Is
there a way to change the value a Windows application exits with. Basically
I want it to return 1 instead of 0. I need to do this because the
application is launched by another application that checked for the exit
code and this particular application expects that the program it launches
to return a non-zero exit code.

Nov 16 '05 #7

P: n/a
Okay, say I have an app called "MyApp" with the following Main function:

static int Main()
{
MessageBox.Show("App starting");
return 1;
}

As you can see, there's not even a main form. If I run this app from a
command shell, e.g.

C:\>MyApp.exe

the message box appears on the screen and the application is obviously
suspended until I clear the dialog, but in the command shell, the next
prompt is already displayed

C:\>MyApp.exe
C:\>

That's what I mean by the application exits immediately. The return code is
always 0 as well:

C:\>MyApp.exe
C:\>echo %errorlevel%
0

So, despite the "return 1" in Main, it has no effect on the return code of
the application. If I change the application type to "Console application"
instead of "Windows application" via the project's property pages, in this
case the the MessageBox call causes the program to suspend execution and the
command shell also hangs until I dismiss the dialog. In this case, the
return code of the app is 1 instead of "0".

So it's clear from my tests that the exit code for a Windows application is
set through some kind of OS magic and it doesn't appear to be able to be
controlled in the normal manner. Is there a way to set it?

"Chris Jobson" <ch**********@btinternet.com> wrote in message
news:uG**************@TK2MSFTNGP15.phx.gbl...
As I understand it the result of Main() is the application's exit code. If
I use Visual Studio to create a C# Windows Application and then modify
Main() to be:
[STAThread]
static int Main() {
Application.Run(new Form1());
return 1;
}
it seems to exit with an exit code of 1. Maybe I'm missing something
because I don't know what you mean by "Windows applications exit
immediately, leaving their windows still open." Surely if there are still
windows open then the application can't have exited (unless the windows
are created by a different thread - I don't know what happens in that case
when the initial thread exits).

Chris Jobson

"Peter Steele" <ps*****@z-force.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I have a Windows application written in C# that I want to return a
non-zero exit code when it it run. The problem is that as a Windows
application, there doesn't seem to be a way to control this. The value
returned by the Main function has no impact on the value returned by the
application. Windows applications exit immediately, leaving their windows
still open. Is there a way to change the value a Windows application exits
with. Basically I want it to return 1 instead of 0. I need to do this
because the application is launched by another application that checked
for the exit code and this particular application expects that the program
it launches to return a non-zero exit code.


Nov 16 '05 #8

P: n/a
Peter Steele wrote:

Okay, say I have an app called "MyApp" with the following Main function:

static int Main()
{
MessageBox.Show("App starting");
return 1;
}

As you can see, there's not even a main form. If I run this app from a
command shell, e.g.

C:\>MyApp.exe
Do this instead, and then report back:

C:\>start /wait MyApp.exe
C:\>echo %errorlevel%


the message box appears on the screen and the application is obviously
suspended until I clear the dialog, but in the command shell, the next
prompt is already displayed

C:\>MyApp.exe
C:\>

That's what I mean by the application exits immediately. The return code is
always 0 as well:

C:\>MyApp.exe
C:\>echo %errorlevel%
0

So, despite the "return 1" in Main, it has no effect on the return code of
the application. If I change the application type to "Console application"
instead of "Windows application" via the project's property pages, in this
case the the MessageBox call causes the program to suspend execution and the
command shell also hangs until I dismiss the dialog. In this case, the
return code of the app is 1 instead of "0".

So it's clear from my tests that the exit code for a Windows application is
set through some kind of OS magic and it doesn't appear to be able to be
controlled in the normal manner. Is there a way to set it?

"Chris Jobson" <ch**********@btinternet.com> wrote in message
news:uG**************@TK2MSFTNGP15.phx.gbl...
As I understand it the result of Main() is the application's exit code. If
I use Visual Studio to create a C# Windows Application and then modify
Main() to be:
[STAThread]
static int Main() {
Application.Run(new Form1());
return 1;
}
it seems to exit with an exit code of 1. Maybe I'm missing something
because I don't know what you mean by "Windows applications exit
immediately, leaving their windows still open." Surely if there are still
windows open then the application can't have exited (unless the windows
are created by a different thread - I don't know what happens in that case
when the initial thread exits).

Chris Jobson

"Peter Steele" <ps*****@z-force.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I have a Windows application written in C# that I want to return a
non-zero exit code when it it run. The problem is that as a Windows
application, there doesn't seem to be a way to control this. The value
returned by the Main function has no impact on the value returned by the
application. Windows applications exit immediately, leaving their windows
still open. Is there a way to change the value a Windows application exits
with. Basically I want it to return 1 instead of 0. I need to do this
because the application is launched by another application that checked
for the exit code and this particular application expects that the program
it launches to return a non-zero exit code.


Nov 16 '05 #9

P: n/a
Okay, this works. It has a side effect though. I need a command that can be
specified in code and

system("start /wait MyApp.exe");

doesn't work. This has to be written as

system("cmd /c\" start /wait MyApp.exe\"");

This has a side effect of throwing up a console window, which is exactly
what I was trying to avoid. I can get around this I guess by using
CreateProcess and telling it to hide the window...

"Julie" <ju***@nospam.com> wrote in message
news:41***************@nospam.com...
Peter Steele wrote:

Okay, say I have an app called "MyApp" with the following Main function:

static int Main()
{
MessageBox.Show("App starting");
return 1;
}

As you can see, there's not even a main form. If I run this app from a
command shell, e.g.

C:\>MyApp.exe


Do this instead, and then report back:

C:\>start /wait MyApp.exe
C:\>echo %errorlevel%


the message box appears on the screen and the application is obviously
suspended until I clear the dialog, but in the command shell, the next
prompt is already displayed

C:\>MyApp.exe
C:\>

That's what I mean by the application exits immediately. The return code
is
always 0 as well:

C:\>MyApp.exe
C:\>echo %errorlevel%
0

So, despite the "return 1" in Main, it has no effect on the return code
of
the application. If I change the application type to "Console
application"
instead of "Windows application" via the project's property pages, in
this
case the the MessageBox call causes the program to suspend execution and
the
command shell also hangs until I dismiss the dialog. In this case, the
return code of the app is 1 instead of "0".

So it's clear from my tests that the exit code for a Windows application
is
set through some kind of OS magic and it doesn't appear to be able to be
controlled in the normal manner. Is there a way to set it?

"Chris Jobson" <ch**********@btinternet.com> wrote in message
news:uG**************@TK2MSFTNGP15.phx.gbl...
> As I understand it the result of Main() is the application's exit code.
> If
> I use Visual Studio to create a C# Windows Application and then modify
> Main() to be:
> [STAThread]
> static int Main() {
> Application.Run(new Form1());
> return 1;
> }
> it seems to exit with an exit code of 1. Maybe I'm missing something
> because I don't know what you mean by "Windows applications exit
> immediately, leaving their windows still open." Surely if there are
> still
> windows open then the application can't have exited (unless the windows
> are created by a different thread - I don't know what happens in that
> case
> when the initial thread exits).
>
> Chris Jobson
>
> "Peter Steele" <ps*****@z-force.com> wrote in message
> news:%2****************@TK2MSFTNGP09.phx.gbl...
>>I have a Windows application written in C# that I want to return a
>>non-zero exit code when it it run. The problem is that as a Windows
>>application, there doesn't seem to be a way to control this. The value
>>returned by the Main function has no impact on the value returned by
>>the
>>application. Windows applications exit immediately, leaving their
>>windows
>>still open. Is there a way to change the value a Windows application
>>exits
>>with. Basically I want it to return 1 instead of 0. I need to do this
>>because the application is launched by another application that checked
>>for the exit code and this particular application expects that the
>>program
>>it launches to return a non-zero exit code.
>
>

Nov 16 '05 #10

P: n/a
Peter Steele wrote:

Okay, this works. It has a side effect though. I need a command that can be
specified in code and

system("start /wait MyApp.exe");

doesn't work. This has to be written as

system("cmd /c\" start /wait MyApp.exe\"");

This has a side effect of throwing up a console window, which is exactly
what I was trying to avoid. I can get around this I guess by using
CreateProcess and telling it to hide the window...


Ok, if that is what you are after, why not just use the following:

Process app = Process.Start("MyApp.exe");
app.WaitForExit(); // or wait for exit event, etc.
int code = app.ExitCode;
Nov 16 '05 #11

P: n/a
I understand what you mean now - but I think the point is that while the
message box is still on the screen the application has NOT exited and so has
NOT yet returned an exit code to Windows (even though it has returned
control to the command prompt, it is still running). Maybe this will be
clearer if you change your code to:
static int Main()
{
MessageBox.Show("App starting");
MessageBox.Show("App stopping");
return 1;
}
You should find that you don't see the second message box until you've
pressed OK on the first one, and similarly the "return 1;" is not executed
until you've pressed OK on the second one.

From this I suspect the problem is with how are you trying to use the exit
code, rather than with how you are setting it. As someone else has
suggested, if you are running it from a command prompt use "start /wait" (if
you type "start /?" the help displayed includes this: "When executing an
application that is a 32-bit GUI application, CMD.EXE does not wait for the
application to terminate before returning to the command prompt. This new
behavior does NOT occur if executing within a command script."). If you're
actually trying to test the exit code in some other way (e.g. from within
another program) then please post an example.

Chris Jobson

----- Original Message -----
From: "Peter Steele" <ps*****@z-force.com>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Tuesday, September 28, 2004 6:11 PM
Subject: Re: How to set exit code of non-console app?

Okay, say I have an app called "MyApp" with the following Main function:

static int Main()
{
MessageBox.Show("App starting");
return 1;
}

As you can see, there's not even a main form. If I run this app from a
command shell, e.g.

C:\>MyApp.exe

the message box appears on the screen and the application is obviously
suspended until I clear the dialog, but in the command shell, the next
prompt is already displayed

C:\>MyApp.exe
C:\>

That's what I mean by the application exits immediately. The return code
is always 0 as well:

C:\>MyApp.exe
C:\>echo %errorlevel%
0

So, despite the "return 1" in Main, it has no effect on the return code of
the application. If I change the application type to "Console application"
instead of "Windows application" via the project's property pages, in this
case the the MessageBox call causes the program to suspend execution and
the command shell also hangs until I dismiss the dialog. In this case, the
return code of the app is 1 instead of "0".

So it's clear from my tests that the exit code for a Windows application
is set through some kind of OS magic and it doesn't appear to be able to
be controlled in the normal manner. Is there a way to set it?

Nov 16 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.