If your so-called "main" thread receives a critical section, and then the VCL thread (which we usually call the "main" thread) tries to receive it, then the VCL thread will block until the critical section is released. Then your "main" thread calls Synchronize , which runs this function in the context of the VCL thread. Since the VCL thread is blocked in the critical section, it does not process messages, so it cannot notice that there is a synchronous method to call. Therefore, a dead end.
Do not hold the lock between calls within the thread. Release the lock in your "main" thread before you call Synchronize , and then acquire it again if you still need to. If the data used in the synchronized method needs constant protection against simultaneous access, I think you should find a way to copy the data to a separate, not shared object. Ask the synchronized method to use this object instead of the general one, and then destroy it later.
Your comment indicates that you can call the callback function without Synchronize , and everything will work. In this case, the synchronized method is not called in the same thread context as before. It is called directly by your "main" thread, not the VCL thread. This explicitly removes the lock conflict, but whether it is really safe depends on the callback function.
source share