Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Application exit code

Tags:

c#

.net

wpf

I am trying to set and get the application exit code .

I am trying to do something following :

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    if ( e.Args.Length != 0)
    {


    }
    else
    {
        new MainWindow().ShowDialog();
    }
    Environment.ExitCode = 110;
    this.Shutdown();
}

And then I am trying in cmd to get it by echo %ERRORLEVEL%

But I get always result 0 , any idea what is the issue ?

like image 394
Night Walker Avatar asked Oct 14 '11 18:10

Night Walker


People also ask

How to Exit application in WPF?

We can close the window either by using "this. Close()"or by using "App. Current. Shutdown()".

How do I close all windows in WPF?

The proper way to shutdown a WPF app is to use Application. Current. Shutdown() . This will close all open Window s, raise some events so that cleanup code can be run, and it can't be canceled.

What does environment exit do?

Exit terminates an application immediately, even if other threads are running. If the return statement is called in the application entry point, it causes an application to terminate only after all foreground threads have terminated. Exit requires the caller to have permission to call unmanaged code.


1 Answers

For WPF, try

Application.Current.Shutdown(110);

Note that the application needs to be running as a console app. This answer is the easiest way I know of; the accepted answer looks more difficult.

An easy test to tell if you're running in console mode: call your app from the command line (make sure your code doesn't shut down right away). The main window should be showing. If you can type another command in the console, your app is not running in its context. The command prompt should be locked, waiting for you to close the window.

like image 141
default.kramer Avatar answered Oct 18 '22 23:10

default.kramer