问题是,您在等待生成的进程退出时阻塞了应用程序的主消息循环,因此在该进程结束之前,您不允许应用程序处理用户输入。你需要让你的应用程序正常处理消息,不要阻止它们。如果在生成的进程运行时禁用表单,用户输入将自动为您放弃。
procedure TForm1.Button1Click(Sender: TObject);
var
StartupInfo : TStartupInfo;
ProcessInfo : TProcessInformation;
CommandLine : string;
begin
CommandLine := 'webmodule.exe';
UniqueString(CommandLine);
ZeroMemory(@StartupInfo, SizeOf(StartupInfo));
StartupInfo.cb := SizeOf(StartupInfo);
if not CreateProcess(PChar(nil), PChar(CommandLine), nil, nil, FALSE, 0, nil, nil, StartupInfo, ProcessInfo) then
begin
ShowMessage('Error : could not execute!');
Exit;
end;
CloseHandle(ProcessInfo.hThread);
Enabled := False;
repeat
case MsgWaitForMultipleObjects(1, ProcessInfo.hProcess, FALSE, INFINITE, QS_ALLINPUT) of
WAIT_OBJECT_0: Break;
WAIT_OBJECT_0+1: Application.ProcessMessages;
else
begin
ShowMessage('Error : could not wait!');
Break;
end;
end;
until False;
CloseHandle(ProcessInfo.hProcess);
Enabled := True;
end;
或者这个:
type
TForm1 = class(ToFrm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
...
private
hWaitObj, hWaitProcess: THandle;
procedure WaitFinished;
...
end;
...
procedure WaitCallback(lpParameter: Pointer; WaitFired: Boolean); stdcall;
begin
TThread.Queue(nil, TForm1(lpParameter).WaitFinished);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
StartupInfo : TStartupInfo;
ProcessInfo : TProcessInformation;
CommandLine : string;
begin
CommandLine := 'webmodule.exe';
UniqueString(CommandLine);
ZeroMemory(@StartupInfo, SizeOf(StartupInfo));
StartupInfo.cb := SizeOf(StartupInfo);
if not CreateProcess(PChar(nil), PChar(CommandLine), nil, nil, FALSE, 0, nil, nil, StartupInfo, ProcessInfo) then
begin
ShowMessage('Error : could not execute!');
Exit;
end;
CloseHandle(ProcessInfo.hThread);
if not RegisterWaitForSingleObject(hWaitObj, ProcessInfo.hProcess, WaitCallback, Self, INFINITE, WT_EXECUTELONGFUNCTION or WT_EXECUTEONLYONCE) then
begin
CloseHandle(ProcessInfo.hProcess);
ShowMessage('Error : could not wait!');
Exit;
end;
hWaitProcess := ProcessInfo.hProcess;
Enabled := False;
end;
procedure TForm1.WaitFinished;
begin
UnregisterWait(hWaitObj);
CloseHandle(hWaitProcess);
Enabled := True;
end;