jpfollenius
jpfollenius

Reputation: 16620

How to change Listener Thread Priority

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?

Upvotes: 2

Views: 1078

Answers (2)

mghie
mghie

Reputation: 32334

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;

Upvotes: 6

Rob Kennedy
Rob Kennedy

Reputation: 163357

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.

Upvotes: 6

Related Questions