Quote Originally Posted by Robert Kosek
I wondered how much a performance difference you could get with the methods, so I dug about and checked. You are correct, the difference is huge. 1.17MS down to 0.0017MS per call. We really need some better thread synchronization techniques in Delphi!
that's normal, .Synchronize method puts the method into a list of the main VCL thread and it gets executed once that thread gets its time slice from the OS...that's the reason of the delay...Windows supports so called APCs (asynchronous procedure calls) which basicly synchronize a method from one thread with the another and does the same thing as Delphi's .Synchronize but on the OS level....drawback of that is it is OS-specific and the thread must be in an alertable wait to process the APC queue which it normally isn't....I don't think you can come up with a better idea of how to execute a method in the context of the target thread....you can force-process the APC queue using undocumented function exported from ntdll.dll - NtAlertThread but I'm not sure if that is acceptable....