DZ-Jay wrote: > Thanks, Wilfried. That's what I'm doing now. However, I'm doing it >> from the end of the Execute() method of the worker thread, right >> before > destroying the server, like this: > > Procedure WorkerThread.Execute; > Begin > _InitializeSrv(); // create > Try > Srv.MessageLoop(); > Finally > Try > For n := 0 to (Srv.ClientCount - 1) Do Begin > Srv.Clients[n].SendStr('bye'#13#10); > Srv.Clients[n].ShutDown(1); > End; > Finally > Srv.Free; > End; > End; > End;
Iterating thru Srv.Clients is not the way to go!! It is not thread-save and will most likely throw strange AVs. Instead iterate thru the thread-list and post a custom message to each thread, the inside the thread iterate thru each thread's client list, see TWSocketThrdServer.DisconnectAll as example. Now one problem is that TWSocketThrdServer has no public property ThreadList and TWsClientThread.PumpMessages doesn't fire an event OnMessage. So the code has to be tweaked a little bit for common use. The only reason why I started writing this component was to derive a SSL multi-threaded server from it only to be able to test thread-safety of the SSL code. > > The problem that I saw was that Calling Shutdown(1) would cause the > server to trigger OnClientDisconnect, which may fire *after* the > thread and client were distroyed by Srv.Free, which will raise an > exception that I would like to avoid if possible. So, what I did was > not call ShutDown(1), and expect the shutdown to occur when > destroying the Srv, like this: > > Try > Srv.Shutdown(0); // make sure we do not accept anybody else! Here Srv.Close is the correct method to call. -- Arno Garrels [TeamICS] http://www.overbyte.be/eng/overbyte/teamics.html -- To unsubscribe or change your settings for TWSocket mailing list please goto http://www.elists.org/mailman/listinfo/twsocket Visit our website at http://www.overbyte.be