🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.delphi.language.objectpascal
Article #32480

Thread sychronization

#32480
From: Ross Shaw
Date: Fri, 22 Nov 2002 14:02
131 lines
3634 bytes
Hi,

I am trying to create a blocking tcp client connection using the TTcpClient
component
(using D7 pro) if in blocking mode I have to keep checking if data it there,
so I have
created a worker thread when a connection is successful that checks and
reads in
data this fires the OnReceive method with the data. It all works but I am
unsure about
sychronization and if its ok to send data with the component when the worker
thread
is checking / reading data.

Thanks for any advice

neehah

code:

TWorkThread = class(TThread)
  private
    FClientSocket: TCustomIpClient;
  protected
    procedure Execute; override;
  public
    constructor Create(ClientSocket: TCustomIpClient); reintroduce; virtual;
    destructor Destroy; override;
  end;

  TTcpThreadClient = class(TCustomIpClient)
  private
    FWorkThread: TWorkThread;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function Connect: Boolean; overload;
    procedure Disconnect overload;
  published
    property Connected;
    property RemoteHost;
    property RemotePort;
    property OnCreateHandle;
    property OnDestroyHandle;
    property OnConnect;
    property OnDisconnect;
    property OnReceive;
    property OnSend;
    property OnError;
  end;

implementation

{ TTcpThreadClient }

function TTcpThreadClient.Connect: Boolean;
var
  Temp: Boolean;
begin

  if Connected then
    Disconnect;

  Temp := inherited Connect;
  file://create the worker thread
  if Temp then
  begin
    if not Assigned(FWorkThread) then
      FWorkThread := TWorkThread.Create(Self);
  end;

  Result := Temp;
end;

constructor TTcpThreadClient.Create(AOwner: TComponent);
begin
  inherited;
  BlockMode := bmBlocking;
end;

destructor TTcpThreadClient.Destroy;
begin
  inherited;
end;

procedure TTcpThreadClient.Disconnect;
begin
  inherited;

  FWorkThread := nil;
end;

{ TWorkThread }

constructor TWorkThread.Create(ClientSocket: TCustomIpClient);
begin
  inherited Create(false);

  FClientSocket := ClientSocket;
  FreeOnTerminate := True;
  Resume;
end;

destructor TWorkThread.Destroy;
begin
  inherited;
end;

procedure TWorkThread.Execute;
const
  BufSize = 32768;
var
  ReadBuf: array[0..BufSize-1] of byte;
begin
  file://inherited; abstract method

  try
    while (not Terminated) and (FClientSocket.Connected) do
    begin
      if FClientSocket.WaitForData(10) then
      begin
        file://we have data read in
        FillChar(ReadBuf, BufSize, 0);
        FClientSocket.ReceiveBuf(ReadBuf, BufSize);
      end;
    end;
  except
    file://deal with exceptions
  end;

end;
Technical Details
Message-ID: <3dde3943$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!sn-xit-08!supernews.com!newshosting.com!news-xfer1.atl.newshosting.com!fu-berlin.de!news.uni-x.net!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 109d30,2a3da90c101926b6,start
X-Google-Attributes: gid109d30,public
X-Google-ArrivalTime: 2002-11-22 06:15:38 PST
From: "Ross Shaw" <rossshaw@no_spampeakndt.com>
Newsgroups: borland.public.delphi.language.objectpascal
Subject: Thread sychronization
Date: Fri, 22 Nov 2002 14:02:18 -0000
Lines: 133
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4807.1700
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
NNTP-Posting-Host: 217.39.227.4
Message-ID: <3dde3943$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1037973827 217.39.227.4 (22 Nov 2002 06:03:47 -0800)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!sn-xit-08!supernews.com!newshosting.com!news-xfer1.atl.newshosting.com!fu-berlin.de!news.uni-x.net!newsgroups.borland.com!not-for-mail