4

I'm trying to get the exit code of my wpf application called in a Powershell script.

My main in WPF :

[STAThread]
public static int Main(string[] args)
{
    if (args != null && args.Length > 0)
    {
        NB_ERRORS = AutomaticTests.Program.Main(args);
        return NB_ERRORS;
        //Application.Current.Shutdown(NB_ERRORS);
    }
    else
    {
        App app = new App();
        app.Run(new MainWindow());

        //Application.Current.Shutdown(NB_ERRORS);
        return NB_ERRORS;
    }
}

And in powershell I call it like this :

& $PathToExeTests 0 $LogTestsStagging
$nbFailed = $LASTEXITCODE

But it always contains 0.

I've tried to manually set the Environment.ExitCode, to shutdown the application with the code, to override OnExit like this :

protected override void OnExit(ExitEventArgs e)
{
    e.ApplicationExitCode = AutomaticTests.GUI.Program.NB_ERRORS;
    base.OnExit(e);
}

But I always have 0 in the LastExitCode.

4
  • the exit code by default is 0...msdn.microsoft.com/en-us/library/… Commented Mar 20, 2017 at 13:57
  • That's why I'm trying to set a custom one to be able to know how many errors I got in my WPF application. Commented Mar 20, 2017 at 14:01
  • 1
    I don`t know in power shell but maybe there is something similar to command line. In command line you have to run the application with "start /wait [Application.exe]" and only then you can use %errorlevel% to get the result back. Maybe power shell has something similar Commented Mar 20, 2017 at 14:04
  • Does this helpl? stackoverflow.com/questions/7772015/… Commented Mar 20, 2017 at 14:28

1 Answer 1

1

By default, when you start a GUI app, PowerShell (just like cmd.exe) will not wait for the app to exit; it'll just tell Windows to start loading the app, and then continue running the script.

There are a couple of ways to wait for a GUI app to exit.

Option 1: Start the app using Start-Process, and then pass the resulting Process object to Wait-Process. This can be easily written as a pipeline:

Start-Process $PathToExeTests -ArgumentList @(0, $LogTestsStaging) | Wait-Process

Option 2: If you do something with the app's standard output (assign it into a variable, or pipe it into another command), then PowerShell will automatically wait for the process to exit.

& $PathToExeTests 0 $LogTestsStaging | Out-Null

Option 1 is probably going to be a lot more readable if someone else is ever going to maintain your code, but occasionally you'll see option 2 as well.

Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't mention exit codes at all but for googlers: for a GUI process, you must -Wait -PassThru - this ensures that the process object will have its $p.ExitCode set correctly. More details here and here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.