MsgWaitForMultipleObjects使用方式 |
|
wangccw
一般會員 ![]() ![]() 發表:21 回覆:39 積分:17 註冊:2005-01-30 發送簡訊給我 |
網路上開啟外部視窗程式並等待執行結束後,回到母視窗,一般都使用 waitForSingleObject ,但它會有執行期間,訊息無法回傳母視窗的問題,所要改用MsgWaitForMultipleObjects,但網路上能找到的 MsgWaitForMultipleObjects 的使用範例實在很少,還是些問題有問題的範例,所以就參考了這程式,再改寫,回饋給 Delphi 的愛好者。 [code delphi] function RunAndWaitForMultipleObjects(const FileName, Params: string; Folder: string; WaitUntilTerminated, WaitUntilIdle, RunMinimized: boolean; var ErrorCode: integer): boolean; var CmdLine: string; WorkingDirP: PChar; StartupInfo: TStartupInfo; ProcessInfo: TProcessInformation; res : Cardinal; Msg : TMsg; begin Result := true; CmdLine := '"' FileName '" ' Params; if Folder = '' then Folder := ExcludeTrailingPathDelimiter(ExtractFilePath(FileName)); ZeroMemory(@StartupInfo, SizeOf(StartupInfo)); StartupInfo.cb := SizeOf(StartupInfo); if RunMinimized then begin StartupInfo.dwFlags := STARTF_USESHOWWINDOW; StartupInfo.wShowWindow := SW_SHOWMINIMIZED; end; if Folder <> '' then WorkingDirP := PChar(Folder) else WorkingDirP := nil; // CreateProcess( nil, PChar( Command ), nil, nil, False, 0, nil, nil, StartupInfo, Result ); if not CreateProcess(nil, PChar(CmdLine), nil, nil, false, 0, nil, WorkingDirP, StartupInfo, ProcessInfo) then begin Result := false; ErrorCode := GetLastError; exit; end; with ProcessInfo do begin if WaitUntilIdle then WaitForInputIdle(hProcess, INFINITE); if WaitUntilTerminated then begin while (true) do begin //使用者對母視窗的操作,全部回應 res := MsgWaitForMultipleObjects( 1, hProcess, False, INFINITE, QS_ALLINPUT ); case res of WAIT_OBJECT_0:begin break; //完成程序 end; WAIT_OBJECT_0 1:begin //還在執行 PeekMessage(Msg, 0, 0, 0, PM_REMOVE); DispatchMessage(Msg); TThread.Sleep(100); //等待 continue; end; else begin break; end; end; break; end; CloseHandle(hProcess); end; end; end; [/code] |
wsx1688
一般會員 ![]() ![]() 發表:0 回覆:0 積分:0 註冊:2024-04-08 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |