在Windows系统中,通过命令行运行可执行程序,通过判断退出码可以辨别程序是否是成功的正常退出。一般情况下,0表示程序正常退出,其他非负数表示退出时出错。
在WPF中的退出事件中,通过设置ExitEventArgs 参数的ApplicationExitCode 属性可以设置退出码。
private void Application_Exit(object sender, ExitEventArgs e)
{
// Assume we have a boolean variable indicating whether the
// application did its work successfully
// Use 0 to indicate success, 1 to indicate that something went wrong
e.ApplicationExitCode = (everythingWorkedOk) ? 0 : 1;
}
你可以通过下面的.bat文件内容测试:
WpfApplication11.exe
REM This evaluates to true if exit code was >= 1
if errorlevel 1 (
echo Failure
) else (
echo Success !
)
echo Actual exit code was %errorlevel%
原文地址:https://wpf.2000things.com/2012/07/26/611-set-application-exit-code-in-exit-event-handler/