[Lazarus] How to PostMessage to object?
Michael Schnell
mschnell at lumino.de
Thu Jun 9 10:44:06 CEST 2011
On 06/08/2011 05:28 PM, Krzysztof wrote:
> Hm, I forgot about Application.QueueAsyncCall. And this can be safely
> call from thread without TThread.Synchronize() ?
This is exactly what Application.QueueAsyncCall() (and what the
Delphi-Worlkalike TThread.Queue() ) does-
> Because I don't want use Synchronize() in any part of thread code. So
> this is correct code? :
>
> procedure TMyThread.Execute;
> begin
> // <some code here>
> Application.QueueAsyncCall(@SomeObj.TestMethod, PtrInt(NewStr('Some
> value')));
> // <some code here>
> end;
here is the code I tested (the thread class offers a proptery
"OnACall_Parameters" as a hook for attaching a Main-Thread handling
procedure):
------------------------------------------------------------------------
Type
TAsyncData = record
S: String;
D: Integer;
end;
TPAsyncData = ^TAsyncData;
TAsyncCall_Parameters = procedure(S: String; D: Integer) of object;
public
OnACall_Parameters: TAsyncCall_Parameters;
var
PAsyncData: TPAsyncData;
begin
new(PAsyncData);
PAsyncData^.d:=Global_I;
PAsyncData^.S:= 'T E S T';
Application.QueueAsyncCall(@DACall_Parameters, PtrInt(PAsyncData));
end;
procedure TWorkerThread.DACall_Parameters(Data: PtrInt);
var
PAsyncData: TPAsyncData;
begin
PAsyncData:=TPAsyncData(Data);
if assigned(OnACall_Parameters) then
OnACall_Parameters(PAsyncData^.S, PAsyncData^.D);
Dispose(PAsyncData);
end;
------------------------------------------------------------------------
-Michael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20110609/ece77b5f/attachment-0003.html>
More information about the Lazarus
mailing list