-
Notifications
You must be signed in to change notification settings - Fork 7
Description
First of all, my compliments to you for this excellent piece of code. It is really neat and quite useful.
There is however an issue when using this in a multi-threading UI environment. The critical section locking is not enough to protect user interface controls. As you know, the Delphi handles (UI) messages in one (main) thread. If you update a control from another thread, even when using (your own) locking mechanism, it is not guaranteed that the main thread is not also accessing the same control at the same time. So this would be only safe if MessageBus is the only one to update the UI. And this is, of course, very unlikely because a user is most likely interacting with the UI, hence the U in UI :-)
One possible solution is to use TThread.Synchronize (TThread.Queue) or even SendMessage (PostMessage) in case the sender is calling from another thread instead of the main thread. You might even implement your own message loop, to ensure this also works in non-UI projects.
In short, the best way would be to always run the Subscribe, Unsubscribe and Send code in the main thread. This would also eliminate the need for a locking mechanism.
Also keep in mind that it is not always safe to create an object (graphic, files access, communication etc) in one thread and destroying it in another thread. Currently MessageBus does not protect you from doing that.
Your thoughts on this?
Regards,
Scott