Tags: app, application, code, exit, net, non-console, non-zero, return, run, sharp, windows, written

How to set exit code of non-console app?

On .Net » .Net C# (C sharp)

25,207 words with 11 Comments; publish: Mon, 02 Jun 2008 19:42:00 GMT; (100140.63, « »)

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.

All Comments

Leave a comment...

  • 11 Comments
    • Try setting Environment.ExitCode

      Thanks,

      Michael C., MCDBA

      "Peter Steele" <psteele.net-csharp.questionfor.info.z-force.com> wrote in message

      news:%23bMdlO9oEHA.1988.net-csharp.questionfor.info.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.

      >

      #1; Mon, 02 Jun 2008 19:43:00 GMT
    • 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.net-csharp.questionfor.info.nospam.org> wrote in message

      news:daC5d.23319$6X1.16559884.net-csharp.questionfor.info.news4.srv.hcvlny.cv.net...

      > Try setting Environment.ExitCode

      > Thanks,

      > Michael C., MCDBA

      > "Peter Steele" <psteele.net-csharp.questionfor.info.z-force.com> wrote in message

      > news:%23bMdlO9oEHA.1988.net-csharp.questionfor.info.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.

      >>

      >

      #2; Mon, 02 Jun 2008 19:44:00 GMT
    • Try putting that line in the .Closing event of your form.

      Thanks,

      Michael C., MCDBA

      "Peter Steele" <psteele.net-csharp.questionfor.info.z-force.com> wrote in message

      news:Oe28Y5%23oEHA.324.net-csharp.questionfor.info.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" <michaelc.net-csharp.questionfor.info.nospam.org> wrote in message

      > news:daC5d.23319$6X1.16559884.net-csharp.questionfor.info.news4.srv.hcvlny.cv.net...

      > > Try setting Environment.ExitCode

      > >

      > > Thanks,

      > > Michael C., MCDBA

      > >

      > > "Peter Steele" <psteele.net-csharp.questionfor.info.z-force.com> wrote in message

      > > news:%23bMdlO9oEHA.1988.net-csharp.questionfor.info.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.

      > >>

      > >>

      > >

      > >

      >

      #3; Mon, 02 Jun 2008 19:45:00 GMT
    • 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.net-csharp.questionfor.info.z-force.com> schrieb im Newsbeitrag

      news:#bMdlO9oEHA.1988.net-csharp.questionfor.info.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.

      >

      #4; Mon, 02 Jun 2008 19:46:00 GMT
    • 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.net-csharp.questionfor.info.gmx.net> wrote in message

      news:uqr49QHpEHA.1588.net-csharp.questionfor.info.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" <psteele.net-csharp.questionfor.info.z-force.com> schrieb im Newsbeitrag

      > news:#bMdlO9oEHA.1988.net-csharp.questionfor.info.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.

      >>

      >

      #5; Mon, 02 Jun 2008 19:47:00 GMT
    • 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.net-csharp.questionfor.info.z-force.com> wrote in message

      news:%23bMdlO9oEHA.1988.net-csharp.questionfor.info.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.

      #6; Mon, 02 Jun 2008 19:48:00 GMT
    • 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.net-csharp.questionfor.info.btinternet.com> wrote in message

      news:uGmvWOOpEHA.3552.net-csharp.questionfor.info.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.net-csharp.questionfor.info.z-force.com> wrote in message

      > news:%23bMdlO9oEHA.1988.net-csharp.questionfor.info.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.

      >

      #7; Mon, 02 Jun 2008 19:49:00 GMT
    • 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" <chris.jobson.net-csharp.questionfor.info.btinternet.com> wrote in message

      > news:uGmvWOOpEHA.3552.net-csharp.questionfor.info.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.net-csharp.questionfor.info.z-force.com> wrote in message

      > > news:%23bMdlO9oEHA.1988.net-csharp.questionfor.info.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.

      > >

      > >

      #8; Mon, 02 Jun 2008 19:50:00 GMT
    • 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.net-csharp.questionfor.info.nospam.com> wrote in message

      news:41599F8C.E1D798E0.net-csharp.questionfor.info.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" <chris.jobson.net-csharp.questionfor.info.btinternet.com> wrote in message

      >> news:uGmvWOOpEHA.3552.net-csharp.questionfor.info.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.net-csharp.questionfor.info.z-force.com> wrote in message

      >> > news:%23bMdlO9oEHA.1988.net-csharp.questionfor.info.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.

      >> >

      >> >

      #9; Mon, 02 Jun 2008 19:51:00 GMT
    • 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;

      #10; Mon, 02 Jun 2008 19:52:00 GMT
    • 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.net-csharp.questionfor.info.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?

      #11; Mon, 02 Jun 2008 19:53:00 GMT