- //线程池结束
-
procedure TThreadsPool.Finished;
-
var
-
bRet:LongBool;
-
begin
-
repeat
-
bRet := SetEvent( FWaitEvent );
-
until bRet;
-
-
repeat
-
//线程池完成标记
-
PoolFineTag := True;
-
until PoolFineTag;
-
-
end;
-
-
-
procedure TThreadsPool.WaitFor;
-
var
-
hHandle:THandle;
-
begin
-
PoolFineTag := False;
-
-
FWaitEvent := CreateEvent(nil, True, False, nil);
-
-
while True do
-
begin
-
hHandle := WaitForSingleObject( FWaitEvent, 100 );
-
-
if ( hHandle = WAIT_OBJECT_0 ) or ( PoolFineTag ) then
-
begin
-
Break;
-
end;
-
-
//Sleep( 100 );
-
end;
-
-
CloseHandle( FWaitEvent );
-
FWaitEvent := 0;
-
end;
这两个函数的作用是在WaitFor中设置阻塞,在Finished中取消阻塞,本来最初两个最关键的地方设置如下:
WaitForSingleObject( FWaitEvent, INFINITE );
SetEvent( FWaitEvent );
按理说这样WaitForSingleObject应该不会永远阻塞吧!但在测试中却发现,有时SetEvent会返回False,所以不得已,把SetEvent放在了循环体中。但是,SetEvent设置成功后,WaitForSingleObject却依然可能永远阻塞,所以又加了了PoolFineTag变量,将WaitForSingleOjbect的第二个参数设置为0。但是,WaitForSingleObject却一直超时,而PoolFineTag为False。所以又将PoolFineTag的设置放入循环体,虽然在改为以前代码后,暂时还没发现一直阻塞的问题,但总觉得问题不应该是这样解决的。
阅读(1461) | 评论(0) | 转发(0) |