Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Aug : How to exit gracefully on listener thread when server crashes.

www.cryer.info
Managed Newsgroup Archive

How to exit gracefully on listener thread when server crashes.

Subject:How to exit gracefully on listener thread when server crashes.
Posted by:"Gerhard Venter" (ieatspam4breakfast@spam.com)
Date:Fri, 18 Aug 2006 10:27:36

I have a listener thread that has a TIdTCPClient connected to a server. The
server runs on Linux and is not a Indy server. I am experiencing problems
that my client app hangs when the server crashes for what ever reason.
Although I have a readtimeout of 5000ms and check for disconnect it still
hangs.

I would like to know how to exit gracefully in this situation. Here is the
code I have on the Run method of  listener's thread (The listener thread is
a TIdThread). If someone can tell me what is wrong or suggest a better way
to do it I would appreciate it.

Thanks
Gerhard

procedure TcsMyListenerThread.Run;
var
  lData: String;
begin
  while not Terminated do
  begin
    if FMyListener.Connected then
    begin
      try
        lData := FMyListener.IOHandler.ReadLn(cCRLF2x, 5000);
        FMyListener.CheckForGracefulDisconnect(True);
        if lData <> EmptyStr then
          FProxyServer.PublishEvent(lData);
      except
        on E:EIdException do
        begin
          if (E.Message = RSNotConnected) or (E.Message =
RSConnectionClosedGracefully) then
            FProxyServer.Stop('Connection to the server has been lost.
Disconnecting.');     <=== (Note:I terminate the thread in the
ProxyServer.Stop method)
        end;
      end;
    end;
  end;
end;

Replies:

www.cryer.info
Managed Newsgroup Archive