2

根据 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;
4

1 回答 1

3

好的。我是个白痴。这一定是假的!

WorkerThread.FreeOnTerminate:=false;
于 2017-08-12T14:10:02.210 回答