For security on GDI streams on Windows, see this help article .
It clearly states that you can safely access descriptors from multiple threads, but this should not be done at the same time. You need to protect access to GDI descriptors, for example. using critical sections.
Remember that GDI descriptors, like most Windows descriptors, are pointers to internal structures associated with integer ( NativeUInt on new Windows, for 64-bit compatibility). As always in multi-threaded computing, accessing the same content simultaneously can be a source of problems that are very difficult to identify and fix.
Part of the user interface of VCL itself should never have been thread safe, from the very beginning, as it relied on an error-free Windows API. For example, if you release a GDI object in a thread that is still needed in another thread, you will encounter a potential GPF.
Embarcadero (at this time) could make the VCL thread safe, serialize all user interface access through critical sections, but it can have additional complexity and reduce overall performance. Please note that even the Microsoft.Net platform (both in WinForms and WPF) also requires a dedicated thread to access the user interface, AFAIK.
So, to update the user interface from multiple threads, you have several templates:
- Use
Synchronize calls from the stream; - Send a special GDI message (see
WM_USER ) from the background threads to notify the user interface thread that an update is required; - You have a stateless approach: the user interface will update its contents from time to time, from a logical level (using a timer or when you press some buttons that can change data).
From my point of view, I prefer option 2 for most user interfaces and option 3 (which can be mixed with option 2) for remote client-server access. Therefore, you do not need the server-side to trigger any update event for the user interface. In the HTTP / AJAX RESTful world, this really makes sense. Option 1 is somewhat slow, IMHO. In all cases, options 2 and 3 expect a clear n-tier multilevel architecture in which logic and interface do not mix: but this is a good template to follow in any case, for any serious development.
Arnaud bouchez
source share