Re: How to set exit code of non-console app?
From: Peter Steele (psteele_at_z-force.com)
Date: 09/28/04
- Next message: Cem Louis: "A C# / SQL Query Problem"
- Previous message: (no email): "server blocks incoming request ???"
- In reply to: Chris Jobson: "Re: How to set exit code of non-console app?"
- Next in thread: Julie: "Re: How to set exit code of non-console app?"
- Reply: Julie: "Re: How to set exit code of non-console app?"
- Reply: Chris Jobson: "Re: How to set exit code of non-console app?"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 28 Sep 2004 10:11:59 -0700
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" <chris.jobson@btinternet.com> wrote in message
news:uGmvWOOpEHA.3552@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" <psteele@z-force.com> wrote in message
> news:%23bMdlO9oEHA.1988@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.
>
>
- Next message: Cem Louis: "A C# / SQL Query Problem"
- Previous message: (no email): "server blocks incoming request ???"
- In reply to: Chris Jobson: "Re: How to set exit code of non-console app?"
- Next in thread: Julie: "Re: How to set exit code of non-console app?"
- Reply: Julie: "Re: How to set exit code of non-console app?"
- Reply: Chris Jobson: "Re: How to set exit code of non-console app?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|