我这有一段别人delfhi的代码,和我要实现的功能完全一样,不知哪位高手能翻译到vc这边:
if CreateProcess(nil, S_CommandLine, @Security, @Security, True, NORMAL_PRIORITY_CLASS, nil, S_WW2000Dir, StartInfo, ProcessInfo) then begin
repeat
I_AppRunning := WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
Application.ProcessMessages;
until (I_AppRunning <> WAIT_TIMEOUT);
end;
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);其实关键就是这句:Application.ProcessMessages
if CreateProcess(nil, S_CommandLine, @Security, @Security, True, NORMAL_PRIORITY_CLASS, nil, S_WW2000Dir, StartInfo, ProcessInfo) then begin
repeat
I_AppRunning := WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
Application.ProcessMessages;
until (I_AppRunning <> WAIT_TIMEOUT);
end;
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);其实关键就是这句:Application.ProcessMessages
Note: ProcessMessages does not allow the application to go idle, whereas HandleMessage does.
建议不要在主线程里用WaitForSingleObject,建一个线程等,当返回时就post一个消息到主窗口或者你主要处理的消息线程就行了。那个delphi代码只是为了方便,代码少,如果用vc来做就麻烦了。function TApplication.ProcessMessage(var Msg: TMsg): Boolean;
var
Handled: Boolean;
begin
Result := False;
if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
begin
Result := True;
if Msg.Message <> WM_QUIT then
begin
Handled := False;
if Assigned(FOnMessage) then FOnMessage(Msg, Handled);
if not IsHintMsg(Msg) and not Handled and not IsMDIMsg(Msg) and
not IsKeyMsg(Msg) and not IsDlgMsg(Msg) then
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end
else
FTerminate := True;
end;
end;