473,386 Members | 1,823 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,386 software developers and data experts.

How to set exit code of non-console app?

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

Similar topics

1
by: Mark McEahern | last post by:
Is there a straightforward way to get the exit code from a windows process? I want to write a script that runs a bunch of .sql files against SQL Server using osql. I want it to stop if there's...
3
by: Micus | last post by:
Greetings, I need to execute 2 applications from an application or batch file. The first app launched has the user enter a product key and the app's exit code will be 0 (failed) or 1...
8
by: Atanas Banov | last post by:
i ran onto this weirdness today: seems like close() on popen-ed (pseudo)file fails miserably with exception instead of returning exit code, when said exit code is -1. here is the simplest...
2
by: iker.arizmendi | last post by:
On UNIX one can use popen* to get a pipe for reading, a pipe for writing, and the exit code of the child process via a call to close() on the last pipe. Is there any way, in principle, to simulate...
4
by: eva.monsen | last post by:
I'm trying to run a .BAT file using System.Diagnostics.Process. I'm having trouble getting Process.ExitCode to match up with what the .BAT file returns. Here are the contents of...
1
by: Liang Zhang | last post by:
Hi, All in my project, i need to call a java method from C code using JNI, yet I do not know how to retrieve the exit code of java program. the java code is like below ..... public static void...
7
by: Benzi Eilon | last post by:
I am executing an Expand command in order to expand a CAB file. This is done by calling CreateProcess with the Expand command and then WaitForSingleObject and checking the process exit code. My C++...
2
by: sunyao | last post by:
Hi Friend, I write a linux shell script to call a python script inside. But I got a wrong exit code from Python exit code. The linux shell script as follows; #!/bin/bash MyPython.py exit $? ...
4
by: Quill_Patricia | last post by:
I have a Python script which is used to load data into a database. Up to now this script has been run by customers from the Windows command prompt using "python edg_loader.pyc". Any error messages...
9
by: titanandrews | last post by:
Hi All, Is there any way to catch the exit code from someone calling exit(status) and check the value before the program terminates? Just for an idea, the code I'm thinking of is something...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.