Talk:Main Loop Hooks

From Lazarus wiki
Revision as of 14:48, 28 April 2006 by Neli (talk | contribs) (code example)

Why is it necessary for the TWaitHandleEvent procedure to be a method of an object? For my simple application it is a lot of unnecessary work to create a class, the method then an instance of it when all I want to do is call a procedure.

Ian Thompson-Bell

It enables OOP code, while not inhibiting pure procedural code; this would not be true the other way around when it would be a simple procedure pointer. Note that you need to define a class with your method, but you do not need to instantiate this class. @YourObject.CallbackMethod with YourObject of your type class and initialized to nil (or not initialized if you prefer) will work as long as you do not use Self in the CallbackMethod (and you don't).

neli 14:29, 27 Apr 2006 (CEST)

Interesting but disappointing. Although I have been programming for 30 years I freely admit to being a newbie at OOP and also to being somewhat sceptical that it is all it is cracked up to be. Apart from the first sentence, your reply is virtualy devoid of meaning to me. I am trying to port an application from Tcl/Tk to Lazarus. It needs to respond to characters arriving at the serial port. In Tcl it is trivial to attach an event handler to incoming serial data,viz

fileevent $serialfd readable rxchar

where fileevent is the command to make the connection, serialfd is the serial port file descriptor, readable is a flag to say watch the input and rxchar is the callback. Surely there must be as simple a way to achieve this with Lazarus.


Ok, maybe a code example will help explain better.
TMyCallbackClass = object
  MyCallback(AData: PtrInt; AFlags: dword);
{ setup callback code }
  InstancePtr: TMyCallbackClass;
  { ... }
  InstancePtr := nil;
  AddEventHandler(SomeHandle, SomeFlags, @InstancePtr.MyCallback, SomeData);
  { ... }

neli 14:48, 28 Apr 2006 (CEST)