Question

A separate thread creates a TidTCPClient and a TTimer. The TTimer is set to 3s and if the TCPClient is not connected, it calls TCPClient.Connect.
If there is no server to connect to, this results in an attempt to connect every 3 seconds.

The main thread (UI) does nothing, but if I grab the window with the mouse and move it slowly across the screen, it get's stuck every 3 seconds for about 2 seconds, then it jumps to the mouse-cursor position and follows the mouse again, until the next attempt to connect occurs.

In other words, the main thread seems to be blocked when the TCPClient tries to connect.

Why does this happen, even though the TCPClient is in it's separate thread?

Was it helpful?

Solution

Your TTimer works by receiving WM_TIMER messages; Those messages are dispatched using the VCL's main message pump, in the VCL threads. After the 3 seconds expire your TTimer.OnTimer event runs in the main thread, so the call to Connect runs in the main VCL thread.

You get blocked because you're not threading!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top