[Lazarus] Windows.PostMessage vs Application.QueueAsyncCall

Marcos Douglas md at delfire.net
Wed Mar 19 02:26:50 CET 2014


On Tue, Mar 18, 2014 at 1:52 PM, Joao Morais <l at joaomorais.com.br> wrote:
> Em 18/03/14 10:47, Marcos Douglas escreveu:
>
>> a form needs to notify many windows
>> using asynchronous messages. PostMessage do that but some programmers
>> say this is an old Windowish approach so, I'm searching another method
>> to do the same PostMessage does and making the code more cross.
>
>
> I missed the "async" part, sorry. What about reuse App.QueueAsyncCall with
> interface?
>
> ======>>>======
>
> type
>   IProcCallback = interface
>     procedure ASync(data: ptrint);
>   end;
>
>   TProc = class(TObject)
>   private
>     FCallback: IProcCallback;
>   public
>     constructor Create(const ACallback: IProcCallback);
>     procedure DoStuff;
>   end;
>
> constructor TProc.Create(const ACallback: IProcCallback);
> begin
>   inherited Create;
>   FCallback := ACallback;
> end;
>
> procedure TProc.DoStuff;
> begin
>   //...
>   Application.QueueAsyncCall(@FCallback.ASync, 0);
> end;
>
> ======<<<======
>
> You can also use {$interfaces corba} if you don't use objects with refcount.
>
> Following this architecture, the responsible for creating TProc instance
> (let's say, TMain) needs to know someone that implements IProcCallback,
> which can be itself (TMain) of course. TMain knows TProc. TProc, TMain and
> others know IProcCallback, and IProcCallback doesn't know anyone.

Ok, this is a good idea -- I'm implementing using concrete classes --
to minimize dependencies... but I still need using an Observer pattern
here because I need a "broadcast message" for many objects.

IMHO there is a "design problem" in PostMessage or QueueAsyncCall:
They are using a Pointer to pass arguments -- pointer or PtrInt as the
same. The caller -- using PostMessage or QueueAsyncCall, doesn't
matter -- doesn't know what valid argument that the receiver will
accept. The programmer could send a Record instance, Object, Integer,
whatever. This is not good. Here using interfaces is good: Encapsulate
the Async(AData: PtrInt) in a interface to each class implement your
own code to (a)sync.

Regards,
Marcos Douglas




More information about the Lazarus mailing list