| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- { $HDR$}
- {**********************************************************************}
- { Unit archived using Team Coherence }
- { Team Coherence is Copyright 2002 by Quality Software Components }
- { }
- { For further information / comments, visit our WEB site at }
- { http://www.TeamCoherence.com }
- {**********************************************************************}
- {}
- { $Log: 22956: ClientThread.pas
- {
- { Rev 1.0 09/10/2003 3:10:54 PM Jeremy Darling
- { Project Checked into TC for the first time
- }
- unit ClientThread;
- interface
- uses
- IdTCPClient,
- ComCtrls,
- Classes,
- SysUtils,
- SyncObjs,
- Windows;
- type
- TClientThread=class(TThread)
- private
- FClient: TIdTCPClient;
- FState: Integer;
- FStatusText: String;
- FListItem: TListItem;
- FLastTick: Cardinal;
- FuiLock: TCriticalSection;
- FSleepTime: Integer;
- procedure SetState(const Value: Integer);
- procedure SetStatusText(const Value: String);
- procedure SetListItem(const Value: TListItem);
- procedure SetuiLock(const Value: TCriticalSection);
- procedure SetSleepTime(const Value: Integer);
- public
- procedure AssignClient(AClient: TIdTCPClient);
- procedure Execute; override;
- destructor Destroy; override;
- property SleepTime : Integer read FSleepTime write SetSleepTime;
- property Client : TIdTCPClient read FClient;
- property State : Integer read FState write SetState;
- property StatusText : String read FStatusText write SetStatusText;
- property ListItem : TListItem read FListItem write SetListItem;
- property uiLock : TCriticalSection read FuiLock write SetuiLock;
- end;
- implementation
- { TClientThread }
- procedure TClientThread.AssignClient(AClient: TIdTCPClient);
- begin
- if not Assigned(FClient) then
- FClient := TIdTCPClient.Create(nil);
- with FClient do
- begin
- Port := AClient.Port;
- Host := AClient.Host;
- IOHandler := AClient.IOHandler;
- OnConnected := AClient.OnConnected;
- OnDisconnected := AClient.OnDisconnected;
- OnStatus := AClient.OnStatus;
- OnWork := AClient.OnWork;
- OnWorkBegin := AClient.OnWorkBegin;
- OnWorkEnd := AClient.OnWorkEnd;
- end;
- end;
- destructor TClientThread.Destroy;
- begin
- uiLock.Enter;
- try
- ListItem.Free;
- finally
- uiLock.Leave;
- FClient.Free;
- inherited;
- end;
- end;
- procedure TClientThread.Execute;
- begin
- while not Terminated do
- begin
- if (not FClient.Connected) and
- (State = -2) then
- begin
- State := -1;
- FClient.Connect;
- FLastTick := GetTickCount;
- end
- else
- begin
- if State <> -1 then
- begin
- if GetTickCount - FLastTick > 1000 then
- begin
- State := State + 1;
- FLastTick := GetTickCount;
- if State > SleepTime then
- begin
- State := -3;
- FClient.Disconnect;
- end;
- end
- else
- Sleep(500);
- end;
- end;
- end
- end;
- procedure TClientThread.SetListItem(const Value: TListItem);
- begin
- FListItem := Value;
- end;
- procedure TClientThread.SetSleepTime(const Value: Integer);
- begin
- FSleepTime := Value;
- end;
- procedure TClientThread.SetState(const Value: Integer);
- begin
- FState := Value;
- case Value of
- -3 : StatusText := 'Disconnecting';
- -2 : StatusText := 'Creating';
- -1 : StatusText := 'Connecting';
- else
- StatusText := 'Sleeping [' + IntToStr(Value) + '/' + IntToStr(SleepTime) + '] while connected';
- end;
- end;
- procedure TClientThread.SetStatusText(const Value: String);
- begin
- uiLock.Enter;
- try
- FStatusText := Value;
- ListItem.SubItems[0] := value;
- finally
- uiLock.Leave;
- end;
- end;
- procedure TClientThread.SetuiLock(const Value: TCriticalSection);
- begin
- FuiLock := Value;
- end;
- end.
|