Save this as killthread.pas
unit killthread;
interface
uses
Classes, IdTCPServer, IdTCPClient, IdContext, Types, SyncObjs;
type
TKillThread = class(TThread)
private
FContext: TIdContext;
FInterval: DWORD;
FEvent: TEvent;
protected
procedure Execute; override;
public
constructor Create(AContext: TIdContext; AInterval: DWORD); overload;
destructor Destroy; override;
procedure Reset;
procedure Stop;
end;
implementation
{ TKillThread }
constructor TKillThread.Create(AContext: TIdContext; AInterval: DWORD);
begin
FContext := AContext;
FInterval := AInterval;
FEvent := TEvent.Create(nil, False, False, '');
inherited Create(False);
end;
destructor TKillThread.Destroy;
begin
FEvent.Free;
inherited Destroy;
end;
procedure TKillThread.Reset;
begin
FEvent.SetEvent;
end;
procedure TKillThread.Stop;
begin
Terminate;
FEvent.SetEvent;
WaitFor;
end;
procedure TKillThread.Execute;
begin
while not Terminated do
begin
if FEvent.WaitFor(FInterval) = wrTimeout then
begin
FContext.Connection.Disconnect;
Exit;
end;
end;
end;
end.
Then do this on the server side:
procedure TYourTCPServer.OnConnect(AContext: TIdContext);
begin
AContext.Data := TKillThread.Create(AContext, 120000);
end;
procedure TYourTCPServer.OnDisconnect(AContext: TIdContext);
begin
TKillThread(AContext.Data).Stop;
end;
procedure TYourTCPServer.OnExecute(AContext: TIdContext);
begin
if AContext.Connection.Connected then
begin
TKillThread(AContext.Data).Reset;
// your code here
end;
end;