Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Oct : Re: TIdTCPServer not shutting down

www.cryer.info
Managed Newsgroup Archive

Re: TIdTCPServer not shutting down

Subject:Re: TIdTCPServer not shutting down
Posted by:"Remy Lebeau (TeamB)" (no.spam@no.spam.com)
Date:Thu, 5 Oct 2006 10:54:02

"Roberto Meneghini" <rmeneghini@separationsystems.com> wrote in message
news:45250fb3$1@newsgroups.borland.com...

> I'm using a class derived from TThread to log events in a TListView.

Why are you using a thread for that?  That is a lot of unnecessary overhead.
I would suggest using a thread-safe container, such as a
TIdThreadSafeStringList, and have the main thread check the container
periodically, such as in a timer.  This way, there is no potential deadlock
areas.  For example:

    var
        Log: TIdThreadSafeStringList;

    constructor TForm1.Create(AOwner: TComponent);
    begin
        inherited Create(AOwner);
        Log := TIdThreadSafeStringList.Create;
    end;

    destructor TForm1.Destroy;
    begin
        IdTCPServer1.Active := False;
        Log.Free;
        inherited Destroy;
    end;

    procedure TForm1.LogerTimerElapsed(Sender: TObject);
    var
        List: TStringList;
    begin
        List := Log.Lock;
        try
            // add contents of List to ListView as needed ...
        finally
            Log.Unlock;
        end;
    end;

    procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
    begin
        Log.Add('Client connected');
    end;

    procedure TForm1.IdTCPServer1Disconnect(AThread: TIdPeerThread);
    begin
        Log.Add('Client disconnected');
    end;

    procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
    begin
        //...
        Log.Add('Client did something');
        //...
    end;

> What I think is happening is that when the server shuts down while a
client
> is still connected, it issues a OnDisconnect. At this point my log thread
is
> created and it tries to updated the TListView using the Synchronize
method.
> However, at this point the form is being destroyed and the thread never
> terminates. Does this sound logical?

Only if the OnDisconnect event is waiting for the log thread to terminate.
In which case, yes it makes perfect sense.  The main thread is not able to
process the Synchronize() request during a shutdown, so the log thread
becomes blocked, which then blocks the OnDisconnect event, which then blocks
the client thread, which then blocks the server from fully shutting down.


Gambit

Replies:

In response to:

www.cryer.info
Managed Newsgroup Archive