Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Dec : Instanciating a second tcpClient

www.cryer.info
Managed Newsgroup Archive

Instanciating a second tcpClient

Subject:Instanciating a second tcpClient
Posted by:"Larry" (lkill..@charter.net)
Date:Sat, 2 Dec 2006 22:41:04

For starters, I am thread challenged.  But Remmy got me this far and it is
working perfectly.  I need to monitor and accept data from a second server.
What is the best way to set up a second client thread (or do I use the same
thread) to receive data from a second server?  Should I instanciate a second
thread using the Delphi thread object as I did the first?  It seems counter
intuitive to do so but I am unable to figure out how it do it any other way.

Please advise.

Thanks
Larry

unit ReadInfeedGSE;

interface

uses
  Classes, IdTCPConnection, StrUtils;

type
  TReadingInfeedThread = class(TThread)
  private
    FConn: TIdTCPConnection;
  protected
    procedure Execute; override;
  public
    procedure AfterConstruction; override;
    constructor Create(AConn: TIdTCPConnection); reintroduce;
    Procedure ShowResults;
  end;


var
  ReadingInfeedThread: TReadingInfeedThread = nil;
  RESULTS : string;
implementation

uses Main, IdIOHandler;

{ Important: Methods and properties of objects in visual components can only
be
  used in a method called using Synchronize, for example,

      Synchronize(UpdateCaption);

  and UpdateCaption could look like,

    procedure TReadingThread.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }

{ TReadingThread }

procedure TReadingInfeedThread.AfterConstruction;
begin
  inherited;
  Resume;
end;

constructor TReadingInfeedThread.Create(AConn: TIdTCPConnection);
begin
  inherited Create(True);
  FConn := AConn;
end;

procedure TReadingInfeedThread.Execute;
var
S: Char;
Buffer : string;
begin
  while not Terminated do
  begin
    S := FConn.IOHandler.ReadChar;
    If (S <> chr(2)) and  (S <> chr(3)) then
    Begin
      Buffer := Buffer + S;
    End
    Else
    Begin
      RESULTS := Buffer;
      Synchronize(ShowResults);
      Buffer := '';
    End;
  end;
end;

procedure TReadingInfeedThread.ShowResults;
begin
   frmMain.GSEoutPut.lines.add(results);
end;

end.

Replies:

www.cryer.info
Managed Newsgroup Archive