Newsgroups : Borland : borland.public.delphi.internet.winsock : 2007 Dec : Re: how to check connection?

www.cryer.info
Managed Newsgroup Archive

Re: how to check connection?

Subject:Re: how to check connection?
Posted by:"Remy Lebeau (TeamB)" (no.spam@no.spam.com)
Date:Wed, 2 Jan 2008 15:16:55

"soonic" <xxsoonic@op.pl> wrote in message
news:477c073a$1@newsgroups.borland.com...

> OK! When I run exe file it works fine. Just when I
> run through delphi, the debugger shows that message.

That is perfectly normal.  Indy is designed to rely on exceptions for its
own internal use.  The debugger is designed to show you any exception that
you have not told it to ignore, before the application can react to it.

> I get this message error in Delphi

Which is no longer an issue.  Ignore it.

> my application freezes when I run it from exe file

That is because you are deadlocking your code.  You are calling WaitFor() in
the context of the reading thread.  The reading thread is waiting on itself.
It will never be able to terminate that way.

You shouldn't even bother trying to disconnect the server when the reading
thread terminates anyway.  Deadlocking asside, that is not a particularly
good design choice.  If you must shut down the server when the client
disconnects, then I suggest you have the reading thread post an asynchronous
message to the main thread (such as via PostMessage(), PostThreadMessage(),
or TIdNotify), and let the main thread handle the shutdown separately.  That
will allow the reading thread to finish terminating itself normally.  For
example:

    procedure TMainForm.DisconnectServer;
    begin
        IdTCPServer.Active := False;
        ....
        if ReadingThread <> nil then ReadingThread.Terminate;
        try
            IdTCPClient.Disconnect;
        finally
            if ReadingThread <> nil then
            begin
                ReadingThread.WaitFor;
                FreeAndNil(ReadingThread);
            end;
        end;
    end;

    procedure TReadingThread.Execute;
    begin
        while (not Terminated) and FConn.Connected do
        begin
            FData.ReadFrom(FConn);
            if not Terminated then Synchronize(UpdateGrid);
        end;
    end;

    procedure TReadingThread.DoTerminate;
    begin
        TIdNotify.NotifyMethod(MainForm.DisconnectServer);
    end;


Gambit

Replies:

In response to:

www.cryer.info
Managed Newsgroup Archive