gpt4 book ai didi

delphi - CreateProcess 、 WaitForSingleObject 、禁用调用应用程序的输入

转载 作者:行者123 更新时间:2023-12-02 03:38:39 28 4
gpt4 key购买 nike

我正在调用另一个仅显示如下网页的程序:

问题:如果我使用按钮创建流程,并且在创建的流程打开时,单击调用表单上的复选框,然后关闭创建的流程,复选框被选中。

我尝试使用DisableTaskWindows(0),如.ShowModal 函数中所示。但它并没有像我预期的那样工作。虽然它确实禁用了表单。但是在我启用它之后,表单似乎无论如何都会处理单击事件。有点像它有消息队列之类的东西。

谁能告诉我我在这里做错了什么?

procedure TForm1.Button1Click(Sender: TObject);
var
StartupInfo : TStartupInfo;
ProcessInfo : TProcessInformation;
ProcessCreated : Boolean;
CommandLine : string;
WindowList: TTaskWindowList;
begin
WindowList := DisableTaskWindows(0);
CommandLine:='webmodule.exe';
uniqueString(CommandLine);
ZeroMemory(@StartupInfo, SizeOf(StartupInfo));
StartupInfo.cb := SizeOf(StartupInfo);
ProcessCreated := CreateProcess(PChar(nil), PChar(CommandLine), nil, nil, false, 0, nil, nil, StartupInfo, ProcessInfo);
if ProcessCreated then
WaitForSingleObject(ProcessInfo.hProcess, INFINITE)
else
ShowMessage('Error : could not execute!');
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
EnableTaskWindows(WindowList);
end;

更新

不幸的是,我不确定如何使用 RegisterWaitForSingleObject 函数...我尝试过这个,但不起作用。我可能错过了回调?但我不知道如何使用它。

  if ProcessCreated then
begin
// WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
while (RegisterWaitForSingleObject(ProcessInfo.hProcess,ProcessInfo.hProcess,nil,nil,INFINITE,0) = false) do
begin
Form1.Color:=RGB(random(255),random(255),random(255));
Application.ProcessMessages;
end;

CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end
else
ShowMessage('Error : could not execute!');

更新2:

我想我可能已经解决了这个问题,我删除了表单的启用禁用。相反,我在执行 Process 后执行此操作。

  while PeekMessage(Msg, 0, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE or PM_NOYIELD) do;
while PeekMessage(Msg, 0, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE or PM_NOYIELD) do;

最佳答案

问题是您在等待生成的进程退出时阻塞了应用程序的主消息循环,因此在该进程结束之前您不允许应用程序处理用户输入。您需要让您的应用正常处理消息,不要阻止它们。如果您在生成的进程运行时禁用表单,则用户输入将自动被丢弃。

尝试更多类似这样的事情:

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;

关于delphi - CreateProcess 、 WaitForSingleObject 、禁用调用应用程序的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54567980/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com