怎么判断线程数(50分)

  • 主题发起人 主题发起人 babieboy
  • 开始时间 开始时间
B

babieboy

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大侠好:<br>&nbsp; &nbsp;小弟现在用TServerSocket.GetThread 事件来做一个线程阻塞的通讯处理,就有这样<br>一个问题,我想在这个事件里先做判断,若先前已创建了20个线程,并且都还没有释放,<br>则不在创建新的线程处理,但不知到有否此判断的api函数,望告知,谢谢。。
 
没有吧。自己用一个threadcount计数呀
 
以下函数得到一个进程的线程数。注意因为是快照,所以不是实时的.<br>function GetCurrentProcessThreadNum:integer;<br>var<br>&nbsp; FSnapshotHandle:THandle;<br>&nbsp; FThreadEntry32:TThreadEntry32;<br>&nbsp; Ret : BOOL;<br>&nbsp; myProcessID:thandle;<br>&nbsp; ThreadNum:integer;<br><br>begin<br>&nbsp; FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,0);<br>&nbsp; FThreadEntry32.dwSize:=Sizeof(FThreadEntry32);<br>&nbsp; Ret:=Thread32First(FSnapshotHandle,FThreadEntry32);<br>&nbsp; ThreadNum := 0;<br>&nbsp; while Ret do<br>&nbsp; begin<br><br>&nbsp; &nbsp; myProcessID := FThreadEntry32.th32OwnerProcessID;<br>&nbsp; &nbsp; if myProcessID = GetCurrentProcessID then<br>&nbsp; &nbsp; &nbsp; &nbsp; ThreadNum := ThreadNum + 1;<br>&nbsp; &nbsp; &nbsp;Ret:=Thread32Next(FSnapshotHandle,FThreadEntry32);<br>&nbsp; end;<br>&nbsp; CloseHandle(FSnapshotHandle);<br>&nbsp; Result := ThreadNum;<br>end;
 
后退
顶部