Comment on one of Rob Kennedy's (old) replies on timers.

From: Martin Harvey (Demon account) (martin_at_nospam_pergolesi.demon.co.uk)
Date: 10/31/04


Date: Sat, 30 Oct 2004 23:47:35 GMT


Rob, way back in March, you wrote this:

>Date: Wed, 24 Mar 2004 11:05:57 -0600
>
>AlanGLLoyd wrote:
>> Encapsulate a windows API Timer in a thread, giving the tthread its
>> own window to receive the messages using Delphi's AllocHWnd function
>> (and DeAllocHWnd - both in Forms.pas).
>
>AllocateHWnd is not thread safe. It calls MakeObjectInstance, which
>keeps a global, unprotected list of all the object methods it has
>wrapped. AllocateHWnd also uses a global TWndClass variable for
>registering window classes.

While this is certainly true, IMHO it misses the point. Although the
call is not thread safe, you can get away with it just fine by
creating and destroying the TThread in the VCL thread, and ensuring
that AllocateHWnd is called in the constructor, and not in the
TThread's execute method.

The reason I mention it is that it's a very common trick to have
either thread objects or objects containing thread objects which use
AllocateHWnd, and although there is a potential issue as you say, I
wouldn't want to peoplle to "get scared" using AllocateHWnd in close
proximity to TThread.

MH.