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?