Talk:Main Loop Hooks

From Lazarus wiki
Jump to navigationJump to search

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.

Ian

Ok, maybe a code example will help explain better.
TMyCallbackObject = class(TObject)
public
  MyCallback(AData: PtrInt; AFlags: dword);
end;
{ setup callback code }
var
  InstancePtr: TMyCallbackObject;
begin
  { ... }
  InstancePtr := nil;
  AddEventHandler(SomeHandle, SomeFlags, @InstancePtr.MyCallback, SomeData);
  { ... }
end;

neli 14:48, 28 Apr 2006 (CEST)

Thanks, that's a little clearer and at least gives me enough to carry on even if I don't fully understand it.

Ian 21:43, 28 Apr 2006 (GMT)