德尔福:等到蝙蝠脚本运行到最后 [英] Delphi: wait until bat-script runs to the end

查看:109
本文介绍了德尔福:等到蝙蝠脚本运行到最后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有BAT文件,使一些操作。如何运行德尔福该文件,并等待,直到它停止。
这样的事情:

 程序TForm1.Button1Click(发件人:TObject的);
开始
//开始BAT文件
喇嘛喇嘛喇嘛
showmessage('完成');
结束;


解决方案

这执行由命令行启动程序给定的命令行,并等待退出。如果程序返回零退出code,如果程序无法启动虚假或返回一个非零的错误code,则返回true。

 函数Exec​​AndWait(常量的CommandLine:字符串):布尔;
VAR
  STARTUPINFO:Windows.TStartupInfo; //启动传递到处理信息
  ProcessInfo:Windows.TProcessInformation; //有关过程信息
  ProcessExit code:Windows.DWord; //进程的退出code
开始
  //设置默认错误结果
  结果:= FALSE;
  //初始化启动信息结构为0,并且记录长度
  FillChar(STARTUPINFO,一下SizeOf(STARTUPINFO),0);
  StartupInfo.cb:=一下SizeOf(STARTUPINFO);
  //执行应用程序的命令行
  如果Windows.CreateProcess(零,PChar类型(的CommandLine)
    零,零,FALSE,0,零,零,
    STARTUPINFO,ProcessInfo),那么
  开始
    尝试
      //现在等待应用程序以完成
      如果Windows.WaitForSingleObject(ProcessInfo.hProcess,INFINITE)
        =然后WAIT_OBJECT_0
        //它完成了 - 获得其退​​出code
        如果Windows.GetExit codeProcess(ProcessInfo.hProcess,
          ProcessExit code),然后
          //检查出code是零= GT;成功完成
          如果ProcessExit code = 0,则
            结果:= TRUE;
    最后
      // 整理
      Windows.CloseHandle(ProcessInfo.hProcess);
      Windows.CloseHandle(ProcessInfo.hThread);
    结束;
  结束;
结束;

从:http://www.delphidabbler.com/$c$csnip?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

这篇关于德尔福:等到蝙蝠脚本运行到最后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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