哈哈,这个问题很典型,虽然很很多人用到线程,但线程很多人控制不好。 TCheLLXThread = class(TThread) private { Private declarations } FTask : TTask ;
FCamDHandle : THandle ;
FNowIndex : Integer ;
FSlpTime : Integer ;
Recbegin
: Cardinal ;
protected Function NowInRecTime : Boolean;
procedure Execute;
override;
public Constructor Create(CAMDHandle: THandle;Task:TTask ;ASlpTime : Integer);
procedure Stop;
end;
procedure TCheLLXThread.Stop;var H: array[0..1] of THandle;
begin
h[0] := self.Handle ;
Terminate;
WaitForSingleObject(H[0], INFINITE);
end;
在 Execute;
过程的循环中 while not terminateddo
beign end;
这个循环不要卡住,让它循环起来。结束时,调用STOP方法就行了。例如:等待一个事件发生procedure TUDPRecvThread.Execute;var dwRc: DWORD;
begin
while not Terminateddo
begin
dwRc := WSAWaitForMultipleEvents( 1, @FEvent, False, FSocket.FWaitForTime, False);
if Terminated then
Break;
{ if (dwRc = WAIT_IO_COMPLETION) or (dwRc = WSA_WAIT_FAILED) then
Break else
} begin
WSAResetEvent(FEvent);
if dwRc = WSA_WAIT_TIMEOUT then
begin
if Assigned(FSocket.FOnTimeOut) then
//设置处理多长时间没有收到数据 Synchronize(FSocket.FOnTimeOut);
end else
begin
if Assigned(FOnDataRecv) then
FOnDataRecv;
{ if Assigned(FOnDataRecv) then
Synchronize(FOnDataRecv);
} end;
end;
end;
end;