procedure TForm1.Button1Click(Sender: TObject);
begin
ShellExecute(handle,'open','cmd.exe',pchar(' /k "dir '+edit1.text+' > c:\lp.txt"'),'',SW_HIDE);
Memo1.Lines.LoadFromFile('c:\lp.txt');
end;
end.
我用以上代码实现运行DOS命令,但C:\lp.txt还没有建立号MEMO就开始加载了,结果总是提示错误,第二次在运行就可以看见正确结果了,有什么办法让他慢一点吗?

解决方案 »

  1.   

    function WinExecAndWait32(FileName:String; Visibility: Integer): Integer;
    var
      zAppName:array[0..512] of char;
      zCurDir:array[0..255] of char;
      WorkDir:String;
      StartupInfo:TStartupInfo;
      ProcessInfo:TProcessInformation;
    begin
      StrPCopy(zAppName,FileName);
      GetDir(0,WorkDir);
      StrPCopy(zCurDir,WorkDir);
      FillChar(StartupInfo,Sizeof(StartupInfo),#0);
      StartupInfo.cb := Sizeof(StartupInfo);  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
      StartupInfo.wShowWindow := Visibility;  if not CreateProcess(nil,
        zAppName,                      { pointer to command line string }
        nil,                           { pointer to process security attributes }
        nil,                           { pointer to thread security attributes }
        false,                         { handle inheritance flag }
        CREATE_NEW_CONSOLE or          { creation flags }
        NORMAL_PRIORITY_CLASS,
        nil,                           { pointer to new environment block }
        nil,                           { pointer to current directory name }
        StartupInfo,                   { pointer to STARTUPINFO }
        ProcessInfo) then Result := -1 { pointer to PROCESS_INF }  else begin
         WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
        //GetExitCodeProcess(ProcessInfo.hProcess,Result);
        Result := 0;
      end;
    end;WinExecAndWait32(cmd,1); //调用
      

  2.   

    这么麻烦,还不如加一句showmessage('open now')