Main Loop Hooks

From Lazarus wiki
Revision as of 23:09, 15 November 2005 by Neli (talk | contribs) (initial text)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Problem: you need to wait for some event (on socket or pipe or ...) to happen, but you want to do this in the main (GUI) thread and do not want to block the GUI and also you do not want multi-threading. Solution to this problem is the ability to add extra "handles" to be watched in the main event loop.

In unit LCLIntf two functions have been added to provide for this functionality, they are:

 AddEventHandler(AHandle: THandle; ACallback: TNotifyEvent);
 RemoveEventHandler(AHandle: THandle);

AddEventHandler adds a handle to be watched to the main event loop. When there is incoming data or a signal, then the procedure specified in ACallback will be called.

RemoveEventHandler stops watching the specified handle.

Win32 supports the following handle types, basically the things supported by MsgWaitForMultipleObjects:

  • change notifications (files)
  • console input
  • events: signalled with SetEvent winapi function
  • mutexes: signalled when it is not owned anymore
  • processes: signalled when they terminate
  • semaphore: signalled when it's count is greater than zero
  • threads: signalled when they terminate
  • timers: signalled when expired, see SetWaitableTimer

Gtk/Unix supports the following handle types:

  • files
  • sockets
  • pipes

Note that win32 does not support sockets or pipes (at least, their use is "discouraged" by MSDN). Suggestion: use WSAEventSelect to create an event associated with a socket and pass the event handle.