The first option we have in order to execute a piece of code (no matter how complex) in the context of the UI thread is to call the TThread.Synchronize method. We'll have a closer look at the TThread class later in this chapter.
The method has two overloaded versions, both with two arguments:
- For both versions, the first argument is a reference to the TThread instance we want to be the executor of code.
- The second argument represents the code portion to be executed; that is, one version accepts a reference to a regular method, while the second version of the method accepts an anonymous method.
We can imagine, as you have seen in Figure 11.1, the worker (secondary) thread will process some code and, at some point (C), reach the need to synchronize with the UI (main) thread. That is the moment that the call to Synchronize has to take place. Without delving too deeply into the internals, we can think/imagine that...