Difference between revisions of "Asynchronous Calls"

From Lazarus wiki
Jump to navigationJump to search
(initial text)
 
(→‎Solution: added links to docs)
Line 5: Line 5:
 
== Solution ==
 
== Solution ==
  
Call Application.QueueAsyncCall:
+
Call Application.[[doc:lcl/forms/tapplication.queueasynccall.html |QueueAsyncCall]]:
  
  TDataEvent = procedure (Data: PtrInt) of object;
+
  [[doc:lcl/forms/tdataevent.html|TDataEvent]] = procedure (Data: PtrInt) of object;
 
   
 
   
  procedure QueueAsyncCall(AMethod: TDataEvent; Data: PtrInt);
+
  procedure [[doc:lcl/forms/tapplication.queueasynccall.html |QueueAsyncCall]](AMethod: TDataEvent; Data: PtrInt);
 
   
 
   
 
This will "queue" the given method with the given parameter for execution in the main event loop, when all other events have been processed. In the example above, the reference to the object you wanted to free has gone, since the then-parent has finished execution, and the object you wanted to free can be freed safely.
 
This will "queue" the given method with the given parameter for execution in the main event loop, when all other events have been processed. In the example above, the reference to the object you wanted to free has gone, since the then-parent has finished execution, and the object you wanted to free can be freed safely.
  
Note that this is a more generic version of ReleaseComponent, and ReleaseComponent calls this method.
+
Note that this is a more generic version of [[doc:lcl/forms/tapplication.releasecomponent.html |ReleaseComponent]], and ReleaseComponent calls this method.

Revision as of 01:18, 13 December 2005

Problem statement

When handling some event, you need to do something, but you can't do it right away. Example: you need to free an object, but it is or will be referenced somewhere in the parent (or its parent etc.) later on.

Solution

Call Application.QueueAsyncCall:

TDataEvent = procedure (Data: PtrInt) of object;

procedure QueueAsyncCall(AMethod: TDataEvent; Data: PtrInt);

This will "queue" the given method with the given parameter for execution in the main event loop, when all other events have been processed. In the example above, the reference to the object you wanted to free has gone, since the then-parent has finished execution, and the object you wanted to free can be freed safely.

Note that this is a more generic version of ReleaseComponent, and ReleaseComponent calls this method.