Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.

ID: 21570, Multithreading - Async Notification library. (V 1.2)

by Martin Harvey Email: Anonymous

A class allowing you to manage asynchronous notifications between worker threads and the VCL thread - Version 1.2.
Download Details
FTP  download also available
CDN Login Required to Download. (You will be redirected to the login page if you click on the Download Link)
To download this, you must have registered:
A free membership

For Delphi, Version 4.0  to 6.0 1143 downloads
Copyright: No significant restrictions

Size: 7,747 bytes
Updated on Tue, 13 Jul 2004 14:08:49 GMT
Originally uploaded on Wed, 24 Mar 2004 15:37:19 GMT
SHA1 Hash: D3FA2A867A15EEC36379F9F2E30736BB31817E21
MD5 Hash: D36E624C215094425F9B9C616DD98A2C

    Explore the files in this upload

If you're familiar with Delphi multithreading, then you'll be aware of the use of "PostMessage" to allow worker threads to asynchronously notify the VCL thread.

If you've been trying to use such mechanisms in custom nonvisual classes, you'll also be aware of the use of AllocateHWnd to allow you to perform your own notifications "in-class".

However, it tends to get a bit tricky for "plug and play" OO code in these situations:

- Threadsafe classes that use this mechanism have to keep track of their own window handles.
- It's not very flexible: notifications are tied to a specific message handling function.
- The use of window handles is tired to the lifetime of the class, not necessarily the threads using it.
- A separate window handle is required for each class instance.
- There's quite a lot of "housekeeping" code to write.

This class takes are of that, and much more. Essentially, it:

- Allows you to obtain a "Notification Handle" to any event or method which can be assigned to a TNotifyEvent.
- Using this handle a thread can then post a notification, which will result in the event handler being called in the context of the VCL thread.
- Additionally, this class also garbage collects notification handles. This means that you can write a threadsafe class which allows notifications from arbitrary many other threads (each with their own handle), and you don't need to worry about keeping track of the handles: each thread will rediscover the handles required to perform notifications on the fly.

A default instance of the class (GlobalNotifier) is created, which should normally be the instance you use. 4 functions exposed by the class are to be used by worker threads:

- AmVCLThread lets a thread know whether it should use these functions, or whether it can just call the event handler directly.

- GetNotifyHandleForEvent will map an event handler or function in the VCL to a notification handle in the worker thread. If the notification handle already exists, then the existing handle is returned, otherwise a new handle is created.

- FreeNotifyHandle unmaps a particular event handler in the VCL from asynchronous notification in the thread.

- Note that you do not necessarily have to keep track of notification handles in your thread: they get cleaned up when your thread quits, and you can always obtain the handle just by knowing which event you want to trigger.

- Also note that different threads should not share handles, instead they should obtain their own notification handle using GetNotifyHandleForEvent

- AsynchronousNotify posts a message from the worker thread to the VCL thread which then calls the event handler.

Version 1.1: bugs fixed

- Occasionally got "WaitForMultipleObjects failed" assertion - slightly erroneous use of the garbage collection function on my part.
- Slightly improved it to deal with handling out of date notifications from threads, in order to prevent AV's which might occur if used in certain ways in a complicated class heirarchy.

Version 1.2: bugs fixed

- If a thread quit, but outstanding messages (out of date notifications) were being delivered, it was possible for the cleanup thread to poll, wasting CPU. Now fixed.

   Latest Comments  View All Add New

Move mouse over comment to see the full text

Could not retrieve comments. Please try again later.

Server Response from: ETNACDC03