views:

486

answers:

2

Hi all! I use the TIdCmdTCPServer component of Indy 10 to realize my client-server application. The problem is that the request from clients can be fairly complicated and that the GUI part of my server application immediately loses responsiveness. From the

Indy Documentation

i got that Indy

Creates and starts listener threads for Bindings using the thread priority tpHighest.

Can I change that behaviour?

+6  A: 

Setting the priority lower for the listening thread won't solve your problem. All the listener thread is doing is listening, which is not a CPU-intensive task. Until a connection arrives, that thread isn't doing anything at all. You may be able to confirm that with a tool like Process Explorer; I think it can show CPU usage by thread.

Setting the priority lower may actually make your server appear less responsive because when a connection arrives, the thread listening for that connection will run with lower priority and won't be able to work on the connection immediately. The client will have to wait a little longer before your server starts processing its request.

The requests are not handled in the listener thread. The listener thread delegates most of the work to other threads. If you have just one TCP binding, then you'll have just one listener thread, but you can process many concurrent connections. Each connection will be handled by a separate thread despite there being only one listener.

Anyway, you can change the priority by handling the server object's OnBeforeListenerRun event. It receives a reference to the TIdThread that represents the listener thread, so you can assign a different value to its Priority property. Also, you have the source code, so you could go in and change the definition of the tpListener constant in IdGlobalCore.pas. The code uses that value, not tpHighest directly.

Rob Kennedy
Hi! Okay, I have to be clearer: the server has a small GUI part that should remain responsive. So what I want (thanks for pointing that out btw) is to lower the priority of the threads created in response to client requests...any whay to do that?
Smasher
+3  A: 

As Rob pointed out, don't change the priority of the listener thread, lower the priority of the worker threads instead. It can be accessed in the OnConnect() handler of the TIdCmdTCPServer instance, like so:

procedure TServerForm.IdCmdTCPServer1Connect(AContext: TIdContext);
begin
  // sanity checks need to go here
  TIdYarnOfThread(AContext.Yarn).Thread.Priority := tpLower;
end;
mghie
That looks exactly like what I'm looking for. I have to wait until tomorrow until I will be able to test it though. I will leave a comment here. Thanks anyway!
Smasher
+1 I just tested it and it works great! Thank you for your help once again!
Smasher