如何在VCL表单应用程序中设置ExitCode [英] How to set ExitCode in a VCL Forms Application

查看:96
本文介绍了如何在VCL表单应用程序中设置ExitCode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用ExitCode来处理VCL表单应用程序。这是我的测试应用程序。它是通过Delphi 2007 IDE中的文件 /新建菜单创建的。唯一的变化是我在行末添加了 ExitCode:= 42;

I can't get ExitCode to work for a VCL forms application. Here is my test application. It was created from the File / New menu in the Delphi 2007 IDE. The only change is that I added the line ExitCode := 42; at the end.

program Test;

uses
  Forms,
  Unit27 in 'Unit27.pas' {Form27};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm27, Form27);
  Application.Run;

  ExitCode := 42;
end.

现在,当我从命令行运行它时,%ERRORLEVEL%未设置:

Now, when I run it from the command line, %ERRORLEVEL% doesn't get set:

>.\Test.exe

>echo %ERRORLEVEL%
0

我期望%ERRORLEVEL%为42,但未更新。

I was expected the value in %ERRORLEVEL% to be 42, but it isn't being updated.

我在控制台应用程序中尝试了相同的实验,效果很好。为什么它对我的GUI应用程序不起作用?

I tried the same experiment in a console application, and that worked fine. Why isn't it working for my GUI application?

推荐答案

您设置退出代码的方法很好。这是您的 test 错误。

Your method of setting the exit code is fine. It's your test that's faulty.

shell不会等待GUI应用程序完成运行,然后提示下一个命令。因此,错误级别已经确定。您正在检查显示命令提示符时的值,而不是在运行 echo 命令时。

The shell doesn't wait for GUI applications to finish running before prompting for the next command. Thus, the error level has already been established. You're checking the value at the time the command prompt was displayed, not at the time you ran the echo command.

在批处理文件或命令脚本中运行程序会修改命令解释器的行为,以使其在运行下一个命令之前等待每个命令完成,即使对于标记为使用GUI子系统的程序也是如此而不是控制台子系统。这就是为什么从批处理文件中正确报告错误级别的原因—在命令解释器获取退出代码之前,您运行的过程已经完成。在不使用命令脚本的情况下,您可以尝试使用 开始命令,并将其传递给 / wait 选项。不过,我不确定它会转发其启动过程的退出代码。

Running programs in a batch file or command script modifies the behavior of the command interpreter to make it wait for each command to finish before running the next one, even for programs marked as using the GUI subsystem instead of the console subsystem. That's why the error level is reported correctly from batch files — the process you ran had finished before the command interpreter fetches the exit code. Without using a command script, you can try starting your program with the start command and passing it the /wait option. I'm not sure it forwards the exit code of the process it starts, though.

这篇关于如何在VCL表单应用程序中设置ExitCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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