2

When I access global variables or the VCL from a TTimer, do I need to use Synchronize? I use the same variables and objects from a TThread, where I already use synchronization.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Artik
  • 803
  • 14
  • 36
  • 2
    It's very hard to understand your question. I would recommend rephrasing to make it clearer. – Svein Bringsli Jan 07 '13 at 14:03
  • Already answered, but to be a bit more clear, a `TTimer` is not a `TThread` in any way. A Timer is a cheap way of performing something repeatedly within its calling thread (in this case the main thread) whereas `Synchronize` is something which is very specific to a `TThread`. – Jerry Dodge Jan 07 '13 at 17:21

1 Answers1

11

TTimer's OnTimer event is executed under the main thread. You don't have to synchronize access to VCL inside that event

iMan Biglari
  • 4,674
  • 1
  • 38
  • 83
  • 8
    Well, to be clearer, the `TTimer.OnTimer` event fires in the context of the thread that creates the `TTimer` instance, as `TTimer` creates an `HWND` internally and thus has thread affinity. If that happens to be the main thread, then the `OnTimer` event will run in the main thread, yes. But if the `TTimer` is created outside of the main thread, then you would have to synchronize. – Remy Lebeau Jan 07 '13 at 18:32