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. | | | | re: How to set exit code of non-console app?
Try setting Environment.ExitCode
Thanks,
Michael C., MCDBA
"Peter Steele" <psteele@z-force.com> wrote in message
news:%23bMdlO9oEHA.1988@TK2MSFTNGP09.phx.gbl...[color=blue]
> I have a Windows application written in C# that I want to return a[/color]
non-zero[color=blue]
> 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.[/color]
Is[color=blue]
> there a way to change the value a Windows application exits with.[/color]
Basically[color=blue]
> 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[/color]
to[color=blue]
> return a non-zero exit code.
>
>[/color] | | | | re: How to set exit code of non-console app?
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" <michaelc@nospam.org> wrote in message
news:daC5d.23319$6X1.16559884@news4.srv.hcvlny.cv. net...[color=blue]
> Try setting Environment.ExitCode
>
> Thanks,
> Michael C., MCDBA
>
> "Peter Steele" <psteele@z-force.com> wrote in message
> news:%23bMdlO9oEHA.1988@TK2MSFTNGP09.phx.gbl...[color=green]
>> I have a Windows application written in C# that I want to return a[/color]
> non-zero[color=green]
>> 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.[/color]
> Is[color=green]
>> there a way to change the value a Windows application exits with.[/color]
> Basically[color=green]
>> 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[/color]
> to[color=green]
>> return a non-zero exit code.
>>
>>[/color]
>
>[/color] | | | | re: How to set exit code of non-console app?
Try putting that line in the .Closing event of your form.
Thanks,
Michael C., MCDBA
"Peter Steele" <psteele@z-force.com> wrote in message
news:Oe28Y5%23oEHA.324@TK2MSFTNGP11.phx.gbl...[color=blue]
> 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[/color]
the[color=blue]
> 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" <michaelc@nospam.org> wrote in message
> news:daC5d.23319$6X1.16559884@news4.srv.hcvlny.cv. net...[color=green]
> > Try setting Environment.ExitCode
> >
> > Thanks,
> > Michael C., MCDBA
> >
> > "Peter Steele" <psteele@z-force.com> wrote in message
> > news:%23bMdlO9oEHA.1988@TK2MSFTNGP09.phx.gbl...[color=darkred]
> >> I have a Windows application written in C# that I want to return a[/color]
> > non-zero[color=darkred]
> >> 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[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> >> Main function has no impact on the value returned by the application.
> >> Windows applications exit immediately, leaving their windows still[/color][/color][/color]
open.[color=blue][color=green]
> > Is[color=darkred]
> >> there a way to change the value a Windows application exits with.[/color]
> > Basically[color=darkred]
> >> 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[/color][/color][/color]
exit[color=blue][color=green][color=darkred]
> >> code and this particular application expects that the program it[/color][/color][/color]
launches[color=blue][color=green]
> > to[color=darkred]
> >> return a non-zero exit code.
> >>
> >>[/color]
> >
> >[/color]
>
>[/color] | | | | re: How to set exit code of non-console app?
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" <psteele@z-force.com> schrieb im Newsbeitrag
news:#bMdlO9oEHA.1988@TK2MSFTNGP09.phx.gbl...[color=blue]
> I have a Windows application written in C# that I want to return a[/color]
non-zero[color=blue]
> 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.[/color]
Is[color=blue]
> there a way to change the value a Windows application exits with.[/color]
Basically[color=blue]
> 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[/color]
to[color=blue]
> return a non-zero exit code.
>
>[/color] | | | | re: How to set exit code of non-console app?
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_spam_deutronium@gmx.net> wrote in message
news:uqr49QHpEHA.1588@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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" <psteele@z-force.com> schrieb im Newsbeitrag
> news:#bMdlO9oEHA.1988@TK2MSFTNGP09.phx.gbl...[color=green]
>> I have a Windows application written in C# that I want to return a[/color]
> non-zero[color=green]
>> 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.[/color]
> Is[color=green]
>> there a way to change the value a Windows application exits with.[/color]
> Basically[color=green]
>> 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[/color]
> to[color=green]
>> return a non-zero exit code.
>>
>>[/color]
>
>[/color] | | | | re: How to set exit code of non-console app?
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...[color=blue]
>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.[/color] | | | | 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?
"Chris Jobson" <chris.jobson@btinternet.com> wrote in message
news:uGmvWOOpEHA.3552@TK2MSFTNGP15.phx.gbl...[color=blue]
> 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...[color=green]
>>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.[/color]
>
>[/color] | | | | re: How to set exit code of non-console app?
Peter Steele wrote:[color=blue]
>
> 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[/color]
Do this instead, and then report back:
C:\>start /wait MyApp.exe
C:\>echo %errorlevel%
[color=blue]
>
> 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...[color=green]
> > 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...[color=darkred]
> >>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.[/color]
> >
> >[/color][/color] | | | | re: How to set exit code of non-console app?
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" <julie@nospam.com> wrote in message
news:41599F8C.E1D798E0@nospam.com...[color=blue]
> Peter Steele wrote:[color=green]
>>
>> 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[/color]
>
> Do this instead, and then report back:
>
> C:\>start /wait MyApp.exe
> C:\>echo %errorlevel%
>
>[color=green]
>>
>> 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...[color=darkred]
>> > 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.
>> >
>> >[/color][/color][/color] | | | | re: How to set exit code of non-console app?
Peter Steele wrote:[color=blue]
>
> 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...[/color]
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; | | | | re: How to set exit code of non-console app?
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" <psteele@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?
[color=blue]
> 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?[/color] |  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|