根据 Microsoft 的文档MAXIMUM_WAIT_OBJECTS
,是64
(等待的最大句柄),但由于某种原因,有时(因为随机)WaitForMultipleObjects
返回WAIT_FAILED
( $FFFFFFFF
) 并GetLastError
返回ERROR_INVALID_HANDLE
( 6
)。我究竟做错了什么?
procedure TForm1.Button1Click(Sender: TObject);
var ArrayOfHandles:array of THandle;
Threads,x:byte;
ReturnValue:Cardinal;
begin
Threads:=64;
SetLength(ArrayOfHandles,Threads);
for x:=0 to Threads-1 do
begin
WorkerThread:= TWorkerThread.Create(True);
ArrayOfHandles[x]:=WorkerThread.Handle;
WorkerThread.FreeOnTerminate:=true;
WorkerThread.Priority := tpNormal;
WorkerThread.Resume;
end;
ReturnValue:=WaitForMultipleObjects(Threads,@ArrayOfHandles[0],false,INFINITE);
ShowMessage('ReturnValue='+IntToStr(ReturnValue)+#13+'GetLastError='+IntToStr(GetLastError));
end;
procedure TWorkerThread.Execute;
begin
sleep( Random(1000) );
end;