gpt4 book ai didi

multithreading - WaitForMultipleObjects 有效,MsgWaitForMultipleObjects 失败 - 为什么?

转载 作者:行者123 更新时间:2023-12-03 15:49:43 26 4
gpt4 key购买 nike

以下代码最低限度地演示了该问题。在后台线程中,我创建一个有效的句柄数组并将其传递给 WaitForMultipleObjects,这成功地等待了对象。

但是,当将完全相同的数组传递给 MsgWaitForMultipleObjects 时,函数调用失败 (WAIT_FAILED),并显示 ERROR_INVALID_HANDLE

我做错了什么?

program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils, Windows, SyncObjs, Classes;

type
TMyThread = class(TThread)
protected
procedure Execute; override;
end;

procedure TMyThread.Execute;
var
LEvent : TEvent;
LWaitHandles : TWOHandleArray;
LPWaitHandles : PWOHandleArray;
LWaitResult : Cardinal;
begin
LEvent := TEvent.Create;
LWaitHandles[0] := LEvent.Handle;
LPWaitHandles := @LWaitHandles;
while not Terminated do begin
{Works -> LWaitResult := WaitForMultipleObjects(1, LPWaitHandles, false, INFINITE);}
{Fails ->} LWaitResult := MsgWaitForMultipleObjects(1, LPWaitHandles, false, INFINITE, QS_ALLINPUT);
case LWaitResult of
WAIT_OBJECT_0: WriteLn('Event 1 Signaled');
{ etc... }
WAIT_FAILED : WriteLn(SysErrorMessage(GetLastError));
end;
end;
end;

var
lt : TMyThread;
begin
lt := TMyThread.Create(false);
ReadLn;
end.

最佳答案

尽管这两个调用的句柄参数的 WinAPI 签名是相同的:

 _In_ const HANDLE *pHandles,

尽管如此,RTL 还是以不同的方式包装了这些函数。 WaitForMultipleObjects 使用指针类型:

lpHandles: PWOHandleArray;

MsgWaitForMultipleObjects 使用无类型 var 参数:

var pHandles;

因此,必须将句柄数组直接传递给 MsgWaitForMultipleObjects

即:

LWaitResult := MsgWaitForMultipleObjects(1, LWaitHandles, false, INFINITE, QS_ALLINPUT);

关于multithreading - WaitForMultipleObjects 有效,MsgWaitForMultipleObjects 失败 - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41812269/

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