WPF应用程序退出代码 [英] WPF Application exit code

查看:179
本文介绍了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();
}

然后我尝试使用 cmd 通过 echo%ERRORLEVEL%

来获取它,但是我总是得到结果 0 ,知道是什么问题吗?

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

推荐答案

对于WPF,请尝试

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.

一个简单的测试来判断您是否在控制台模式下运行:从命令行调用应用程序(确保您的代码没有t立即关闭)。主窗口应显示。如果您可以在控制台中键入其他命令,则您的应用程序未在其上下文中运行。命令提示符应该被锁定,等待您关闭窗口。

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.

这篇关于WPF应用程序退出代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆