Synchronize and Queue
The second and third approaches are so similar that I've condensed them into one section.
The setup for this approach is the same as with the first shown in the Windows message method—we only have to initialize FValue and FNumTasks. The code in increment and decrement tasks also starts the same, with a for loop that increments or decrements the value but then uses a different approach to pass the value back to the main thread.
The following code uses TThread.Synchronize to execute an anonymous method in the target thread. Synchronize accepts a TThread object as a first parameter and an anonymous method in the second. It will execute the second parameter in the main thread. In the first parameter, we can pass either the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access