This can work but need another mechanism for passing parameters to the thread. You can set an event where the thread on waiting on. It is however difficult to pass parameters when setting the event. You could use a PostThreadMessage function to get the same thing and it always you to pass information in the message structure.
You will need a mechanism to find out for which thread the ClientRead is intended since you will have 20 clients and therefore 20 threads. However: there is another approach where the thread is creating the client socket itself and therefore it is not necessary that the service thread captures the OnClientRead events. The client thread could do it by its own. This is a bit more difficult to do since you have to capture the socket events. An easier approach is to use a form from whithin the thread (so you will have 20 forms) so message handling is easier. kind regards, Ronny Aerts ----- Original Message ----- From: dts124 To: [email protected] Sent: Sunday, April 22, 2007 8:29 AM Subject: [Norton AntiSpam] [delphi-en] Re: service with threads for socket connections there are more than 20 clients asking for data at random time. I wanted simultaneous connections serve immediately. My be i really do not need it, but i want to try. I try to send OnClientRead event to the thread like this: procedure TService1.ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket); ... NewThread.Event := ServerSocket1.OnClientRead(Self, ?); but what i must put as parameters? In the thread: type TEchoThread = class(TThread) private { Private declarations } FEvent: Integer; ... public property Event: integer write FEvent; ... while not Terminated do // loop around until we should stop begin if WaitForSingleObject(FEvent, INFINITE) = WAIT_OBJECT_0 then begin ... Can this work? Kind regards, Dmitri --- In [email protected], "Ronny Aerts [grasoft]" <[EMAIL PROTECTED]> wrote: > > You don't need a thread for every tcp/ip socket connection. When your application works fine in a form, you can reuse this form when create and activate it from the start of service. > > kind regards, > Ronny Aerts > Belgium > > ----- Original Message ----- > From: dts124 > To: [email protected] > Sent: Saturday, April 21, 2007 1:37 PM > Subject: [Norton AntiSpam] [delphi-en] Re: service with threads for socket connections > > > --- In [email protected], "dts124" <dmitri.tsvetikov@> wrote: > > there was a suggestion to use WaitForSingleObject for thread to wait > for next message: > > procedure TEchoThread.Execute; > var > Str: string; > begin > while not Terminated do > begin > if WaitForSingleObject(SomeEvent, INFINITE) = WAIT_OBJECT_0 then > begin > begin > Str := IntToStr(FSockHnd) +' '+ IntToStr(FConnIndx) +' '+ FBuff; > > Service1.ServerSocket1.Socket.Connections[FConnIndx].SendText(Str); > end; > end; > end; > end; > > Question: how to pass ServerSocket1 OnClientRead event of the main > thread to WaitForSingleObject function of thread? > > dts124 > ------------------------------ > > > > I am trying to port a working "Form" application (medical DICOM > > worklist server) to service. The application is answering to multiple > > possibly concurrent connections, so i assume there must be separate > > thread for each existing connection. Connections (and threads) are > > closed after the requested data is sent. > > > > For now my test service just echoes back text string received from > > client with addition of connection handle number. > > > > Problem: > > after 1-st client connection CPU usage of this service is 50%(probably > > because my CPU is Pentium D), after 2nd - 99% and remains 99% until > > service is stopped regardless that all clients are disconnected. > > > > After i added Sleep(10); into > > "while not Terminated do" loop of procedure TEchoThread.Execute; > > CPU usage dropped to 0, now it seems OK, but is this the right way to > > arrange service with threads for socket connections? > > > > > > > > [Non-text portions of this message have been removed] > [Non-text portions of this message have been removed]

