gpt4 book ai didi

delphi - 暂停程序,直到外部程序退出

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

我正在编写一些代码来还原备份。为此,我使用一个外部程序。用户可以选择多个文件,我将在for循环内为每个文件执行外部程序。在还原下一个文件之前,如何暂停程序直到外部程序退出?

最佳答案

这是使用CreateProcess()和WaitForSingleObject()的示例

procedure RunaAndWait(Command:String);
var
fStartupInfo: TStartupInfo;
fProcessInformation: TProcessInformation;
fProgram: String;
begin
fProgram := trim(Command);
FillChar(fStartupInfo, SizeOf(fStartupInfo), 0);
with fStartupInfo do
begin
cb := SizeOf(TStartupInfo);
wShowWindow := SW_HIDE;
end;

if CreateProcess(nil, pchar(fProgram), nil, nil, true, CREATE_NO_WINDOW,
nil, nil, fStartupInfo, fProcessInformation) then
begin
while WaitForSingleObject(fProcessInformation.hProcess, 10) > 0 do
begin
Application.ProcessMessages;
end;
CloseHandle(fProcessInformation.hProcess);
CloseHandle(fProcessInformation.hThread);
end
else
begin
RaiseLastOSError;
end;
end;


用法:

procedure TForm1.Button1Click(Sender: TObject);
begin
RunaAndWait('notepad.exe');
Showmessage('Finished');
end;

关于delphi - 暂停程序,直到外部程序退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35306320/

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