根据Microsoft的文档,MAXIMUM_WAIT_OBJECTS不能大于64,但是由于某些原因(由于随机),有时WaitForMultipleObjects返回WAIT_FAILED(4294967295),而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;

最佳答案

好。我是个白痴。这必须是错误的!

WorkerThread.FreeOnTerminate:=false;

08-25 21:24