Delphi:等到蝙蝠脚本运行到最后 [英] Delphi: wait until bat-script runs to the end
本文介绍了Delphi:等到蝙蝠脚本运行到最后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这样的东西:
procedure TForm1.Button1Click(Sender:TObject);
begin
//开始蝙蝠文件
bla-bla-bla
showmessage('Done');
结束
解决方案
这将执行给定的命令行并等待程序由命令行启动退出。如果程序返回零退出代码,则返回true,如果程序未启动或返回非零错误代码,则返回false。
function ExecAndWait(const CommandLine:string):Boolean;
var
StartupInfo:Windows.TStartupInfo; //启动信息传递给进程
ProcessInfo:Windows.TProcessInformation; //关于进程的信息
ProcessExitCode:Windows.DWord; //进程的退出代码
begin
//设置默认错误结果
结果:= False;
//初始化启动信息结构为0,记录长度
FillChar(StartupInfo,SizeOf(StartupInfo),0);
StartupInfo.cb:= SizeOf(StartupInfo);
//执行应用程序命令行
如果Windows.CreateProcess(nil,PChar(CommandLine),
nil,nil,False,0,nil,nil,
StartupInfo,ProcessInfo)then
begin
try
//现在等待应用程序完成
如果Windows.WaitForSingleObject(ProcessInfo.hProcess,INFINITE)
= WAIT_OBJECT_0然后
//完成 - 如果Windows.GetExitCodeProcess(ProcessInfo.hProcess,
ProcessExitCode)然后
//检查退出代码为零=>获取其退出代码
成功完成
如果ProcessExitCode = 0然后
结果:= True;
finally
// Tidy up
Windows.CloseHandle(ProcessInfo.hProcess);
Windows.CloseHandle(ProcessInfo.hThread);
结束
结束
结束
来自: http://www.delphidabbler.com/codesnip?action=named&showsrc=1&routines=ExecAndWait
I have bat-file, that make some operations. How to run this file from Delphi and wait, until it stops. Something like that:
procedure TForm1.Button1Click(Sender: TObject);
begin
//Starting bat-file
bla-bla-bla
showmessage('Done');
end;
解决方案
This executes the given command line and waits for the program started by the command line to exit. Returns true if the program returns a zero exit code and false if the program doesn't start or returns a non-zero error code.
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;
From: http://www.delphidabbler.com/codesnip?action=named&showsrc=1&routines=ExecAndWait
这篇关于Delphi:等到蝙蝠脚本运行到最后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文