gpt4 book ai didi

Delphi:WAITINGbat脚本运行结束

转载 作者:行者123 更新时间:2023-12-03 15:22:45 24 4
gpt4 key购买 nike

我有bat文件,可以进行一些操作。如何从 Delphi 运行该文件并等待,直到它停止。类似这样的事情:

procedure TForm1.Button1Click(Sender: TObject);
begin
//Starting bat-file
bla-bla-bla
showmessage('Done');
end;

最佳答案

这将执行给定的命令行并等待命令行启动的程序退出。如果程序返回零退出代码,则返回 true;如果程序未启动或返回非零错误代码,则返回 false。

function ExecAndWait(const CommandLine: string) : Boolean;
var
StartupInfo: Windows.TStartupInfo; // start-up info passed to process
ProcessInfo: Windows.TProcessInformation; // info about the process
ProcessExitCode: Windows.DWord; // process's exit code
begin
// Set default error result
Result := False;
// Initialise startup info structure to 0, and record length
FillChar(StartupInfo, SizeOf(StartupInfo), 0);
StartupInfo.cb := SizeOf(StartupInfo);
// Execute application commandline
if Windows.CreateProcess(nil, PChar(CommandLine),
nil, nil, False, 0, nil, nil,
StartupInfo, ProcessInfo) then
begin
try
// Now wait for application to complete
if Windows.WaitForSingleObject(ProcessInfo.hProcess, INFINITE)
= WAIT_OBJECT_0 then
// It's completed - get its exit code
if Windows.GetExitCodeProcess(ProcessInfo.hProcess,
ProcessExitCode) then
// Check exit code is zero => successful completion
if ProcessExitCode = 0 then
Result := True;
finally
// Tidy up
Windows.CloseHandle(ProcessInfo.hProcess);
Windows.CloseHandle(ProcessInfo.hThread);
end;
end;
end;

来自:http://www.delphidabbler.com/codesnip?action=named&showsrc=1&routines=ExecAndWait

关于Delphi:WAITINGbat脚本运行结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2443886/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com