Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取powershell中wpf应用程序的退出代码_C#_Wpf_Powershell - Fatal编程技术网

C# 获取powershell中wpf应用程序的退出代码

C# 获取powershell中wpf应用程序的退出代码,c#,wpf,powershell,C#,Wpf,Powershell,我正在尝试获取在Powershell脚本中调用的wpf应用程序的退出代码 我在WPF的主要工作: [STAThread] public static int Main(string[] args) { if (args != null && args.Length > 0) { NB_ERRORS = AutomaticTests.Program.Main(args); return NB_ERRORS; //

我正在尝试获取在Powershell脚本中调用的wpf应用程序的退出代码

我在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;
    }
}
在powershell中,我这样称呼它:

& $PathToExeTests 0 $LogTestsStagging
$nbFailed = $LASTEXITCODE
protected override void OnExit(ExitEventArgs e)
{
    e.ApplicationExitCode = AutomaticTests.GUI.Program.NB_ERRORS;
    base.OnExit(e);
}
但它总是包含0

我尝试手动设置
环境.ExitCode
,用代码关闭应用程序,覆盖OnExit,如下所示:

& $PathToExeTests 0 $LogTestsStagging
$nbFailed = $LASTEXITCODE
protected override void OnExit(ExitEventArgs e)
{
    e.ApplicationExitCode = AutomaticTests.GUI.Program.NB_ERRORS;
    base.OnExit(e);
}

但是我在LastExitCode中总是有0。

默认情况下,当您启动GUI应用程序时,PowerShell(就像cmd.exe)不会等待应用程序退出;它只会告诉Windows开始加载应用程序,然后继续运行脚本

有几种方法可以等待GUI应用程序退出

选项1:使用
启动流程
启动应用程序,然后将生成的流程对象传递给
等待流程
。这可以很容易地写成管道:

Start-Process $PathToExeTests -ArgumentList @(0, $LogTestsStaging) | Wait-Process
选项2:如果您对应用程序的标准输出执行某些操作(将其分配到一个变量中,或将其输送到另一个命令中),则PowerShell将自动等待进程退出

& $PathToExeTests 0 $LogTestsStaging | Out-Null

如果其他人要维护您的代码,选项1可能会更具可读性,但偶尔您也会看到选项2。

默认情况下退出代码为0…这就是为什么我试图设置一个自定义代码,以便能够知道我在WPF应用程序中遇到了多少错误。我不知道power shell中有多少错误,但可能与命令行类似。在命令行中,您必须使用“start/wait[application.exe]”运行应用程序,然后才能使用%errorlevel%返回结果。也许power shell也有类似的东西这有帮助吗?