全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1340
推到 Plurk!
推到 Facebook!

如何等待其他程式執行完畢的問題

尚未結案
rachex2000
一般會員


發表:21
回覆:32
積分:20
註冊:2003-04-28

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-04-17 12:53:35 IP:210.64.xxx.xxx 未訂閱
我有一程式開啟後再按鈕會執行一pack.exe,因為必須等到pack.exe執行完畢後才執行下一步,因此使用createprocess與waitforsingleobject,可是發現若是以 mouse在檔案總管中開啟可以正常執行,若是以命令字元執行例C:\>C:\A\project1.exe執行,pack.exe似乎不會啟動,很快就跳到下一步了,因為此程式也要由其他程式呼叫開啟,因此一定要解決此問題  
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  runCommand('pack.exe 01', false, true);  //01為傳入pack.exe的參數      showmessage('T');
end;    {
  sCmdLine: 要執行的命令列, 包含命令與參數
  isShow: 要顯示出視窗否?
  isWait: 是否要等待被執行的命令執行完畢?
}    function runCommand(sCmdLine: string; isShow: boolean = true; isWait: boolean = true): boolean;
var
  _oSecurity: TSecurityAttributes;
  _oStartInfo: TStartUpInfo;
  _oProcessInfo: TProcessInformation;
  _wAppRunning: DWord;
  _isOK: boolean;
begin
  with _oSecurity do
  begin
    nlength := SizeOf(TSecurityAttributes);
    binherithandle := true;
    lpsecuritydescriptor := nil;
  end;      // Set up members of the PROCESS_INFORMATION structure.
  ZeroMemory(@_oProcessInfo, sizeof(TPROCESSINFORMATION));      // Set up members of the _oStartInfoUPINFO structure.
  FillChar(_oStartInfo, Sizeof(_oStartInfo), #0);
  _oStartInfo.cb := SizeOf(_oStartInfo);
  _oStartInfo.dwFlags := STARTF_USESTDHANDLES   STARTF_USESHOWWINDOW;
  if (isShow) then
    _oStartInfo.wShowWindow := SW_SHOWNORMAL
  else
    _oStartInfo.wShowWindow := SW_HIDE;      _isOK := CreateProcess(nil,
    PChar(sCmdLine),
    @_oSecurity,
    @_oSecurity,
    true,
    NORMAL_PRIORITY_CLASS,
    nil,
    nil,
    _oStartInfo,
    _oProcessInfo);      if (_isOK) and (isWait) then
  begin
    repeat
      _wApprunning := WaitForSingleObject(_oProcessInfo.hProcess, 100);
      Application.ProcessMessages;
    until (_wApprunning <> WAIT_TIMEOUT);
  end;      result := _isOK;
end;    
Chance36
版主


發表:31
回覆:1033
積分:792
註冊:2002-12-31

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-04-18 02:57:28 IP:203.204.xxx.xxx 未訂閱
[quote] rachex2000 你好 我找了兩個論內容,你參考看看應該可以符合你的需求 http://delphi.ktop.com.tw/topic.php?TOPIC_ID=38858 http://delphi.ktop.com.tw/topic.php?TOPIC_ID=20984
rachex2000
一般會員


發表:21
回覆:32
積分:20
註冊:2003-04-28

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-04-20 15:31:01 IP:210.64.xxx.xxx 未訂閱
發現解決辦法了,createprocess的LPCTSTR lpCurrentDirectory參數要 傳入extractfilePath(Application.exename),若傳入nil,會以程式呼叫處的路徑傳入, 若是使用shellexecute也有同樣情形
系統時間:2024-05-21 21:19:36
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!