Difference between revisions of "Main Loop Hooks"

From Lazarus wiki
Jump to navigationJump to search
(pipe and process event handlers)
(API update)
Line 7: Line 7:
 
In unit [[doc:lcl/lclintf|LCLIntf]] two functions have been added to provide for this functionality, they are:
 
In unit [[doc:lcl/lclintf|LCLIntf]] two functions have been added to provide for this functionality, they are:
  
  AddEventHandler(AHandle: THandle; AFlags: dword;  
+
AddEventHandler(AHandle: THandle; AFlags: dword;  
                  AEventHandler: TWaitHandleEvent; AData: PtrInt);
+
    AEventHandler: TWaitHandleEvent; AData: PtrInt): PEventHandler;
  RemoveEventHandler(AHandle: THandle);
+
RemoveEventHandler(AHandler: PEventHandler);
 +
SetEventHandlerFlags(AHandler: PEventHandler; NewFlags: dword);
  
 
The [[doc:lcl/interfacebase/twaithandleevent.html | TWaitHandleEvent]] type is declared in unit InterfaceBase as:
 
The [[doc:lcl/interfacebase/twaithandleevent.html | TWaitHandleEvent]] type is declared in unit InterfaceBase as:
Line 16: Line 17:
  
 
[[doc:lcl/lclintf/addeventhandler.html | AddEventHandler]] adds a handle to be watched to the main event loop.  
 
[[doc:lcl/lclintf/addeventhandler.html | AddEventHandler]] adds a handle to be watched to the main event loop.  
When the handle is "signalled", then the procedure specified in ACallback will be called with as a parameter the AData that was passed when registering the event handler, and any Flags, which are OS specific.
+
When the handle is "signalled", then the procedure specified in ACallback will be called with as a parameter the AData that was passed when registering the event handler, and any Flags, which are OS specific. The flags passed to AddEventHandler can be modified using SetEventHandlerFlags. The returned handler (a pointer) by AddEventHandler should be passed to RemoveEventHandler to stop watching the associated handle.
  
 
[[doc:lcl/lclintf/removeeventhandler | RemoveEventHandler]] stops watching the specified handle.
 
[[doc:lcl/lclintf/removeeventhandler | RemoveEventHandler]] stops watching the specified handle.
Line 51: Line 52:
 
To provide a cross-platform solution for pipes (not well supported on win32) and processes (not well supported on gtk/unix) additional functions have been added:
 
To provide a cross-platform solution for pipes (not well supported on win32) and processes (not well supported on gtk/unix) additional functions have been added:
  
  TChildExitReason = (cerExit, cerSignal);
+
TChildExitReason = (cerExit, cerSignal);
  TPipeReason = (prDataAvailable, prBroken, prCanWrite);
+
TPipeReason = (prDataAvailable, prBroken, prCanWrite);
  TPipeReasons = set of TPipeReason;
+
TPipeReasons = set of TPipeReason;
 
+
  TChildExitEvent = procedure(AData: PtrInt; AReason: TChildExitReason; AInfo: dword) of object;
+
TChildExitEvent = procedure(AData: PtrInt; AReason: TChildExitReason; AInfo: dword) of object;
  TPipeEvent = procedure(AData: PtrInt; AReasons: TPipeReasons) of object;
+
TPipeEvent = procedure(AData: PtrInt; AReasons: TPipeReasons) of object;
  
  procedure AddProcessEventHandler(AHandle: THandle; AEventHandler: TChildExitEvent; AData: PtrInt);
+
function  AddProcessEventHandler(AHandle: THandle;  
  procedure RemoveProcessEventHandler(AHandle: THandle);
+
    AEventHandler: TChildExitEvent; AData: PtrInt): PProcessEventHandler;
 
+
procedure RemoveProcessEventHandler(AHandler: PProcessEventHandler);
  procedure AddPipeEventHandler(AHandle: THandle; AEventHandler: TPipeEvent; AData: PtrInt);
+
  procedure RemovePipeEventHandler(AHandle: THandle);
+
function  AddPipeEventHandler(AHandle: THandle;  
 +
    AEventHandler: TPipeEvent; AData: PtrInt): PPipeEventHandler;
 +
procedure RemovePipeEventHandler(AHandler: PPipeEventHandler);
  
 
When a process terminates the event handler specified will be called. AInfo will contain the exit code if AReason is cerExit, or (on unix only) the termination signal if AReason is cerSignal. For gtk/unix, use the PID to watch as AHandle. Internally, a signal handler is installed to catch the SIGCHLD signal. On win32, AddEventHandler is used to watch process termination.
 
When a process terminates the event handler specified will be called. AInfo will contain the exit code if AReason is cerExit, or (on unix only) the termination signal if AReason is cerSignal. For gtk/unix, use the PID to watch as AHandle. Internally, a signal handler is installed to catch the SIGCHLD signal. On win32, AddEventHandler is used to watch process termination.
  
 
To watch a pipe for incoming data, pass the file descriptor (unix) or the pipe read-end handle to AddPipeEventHandler with a custom chosen event handler method as AEventHandler. On gtk/unix, AddEventHandler is called to watch the file descriptor for traffic. On win32, the message loop will timeout every 100 msecs to check all watched pipes for available data; if data is available, the event handler is called.
 
To watch a pipe for incoming data, pass the file descriptor (unix) or the pipe read-end handle to AddPipeEventHandler with a custom chosen event handler method as AEventHandler. On gtk/unix, AddEventHandler is called to watch the file descriptor for traffic. On win32, the message loop will timeout every 100 msecs to check all watched pipes for available data; if data is available, the event handler is called.

Revision as of 21:25, 7 January 2006

Problem statement

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.

Solution details

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

AddEventHandler(AHandle: THandle; AFlags: dword; 
    AEventHandler: TWaitHandleEvent; AData: PtrInt): PEventHandler;
RemoveEventHandler(AHandler: PEventHandler);
SetEventHandlerFlags(AHandler: PEventHandler; NewFlags: dword);

The TWaitHandleEvent type is declared in unit InterfaceBase as:

 TWaitHandleEvent = procedure(AData: PtrInt; AFlags: dword) of object;

AddEventHandler adds a handle to be watched to the main event loop. When the handle is "signalled", then the procedure specified in ACallback will be called with as a parameter the AData that was passed when registering the event handler, and any Flags, which are OS specific. The flags passed to AddEventHandler can be modified using SetEventHandlerFlags. The returned handler (a pointer) by AddEventHandler should be passed to RemoveEventHandler to stop watching the associated handle.

RemoveEventHandler stops watching the specified handle.

Windows

The AFlags parameter in AddEventHandler is unused, and the AFlags in TWaitHandleEvent will be 0, in the current implementation.

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

  • change notifications (for files and directories)
  • 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

The AFlags parameter in AddEventHandler specifies the condition in which the handle should be signalled, with the possible values being the ones documented in the GIOCondition type in the glib reference.

In the callback, the AFlags will contain the condition, of the above referenced GIOCondition type, that was satisfied.

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.

Pipes and Process Termination

To provide a cross-platform solution for pipes (not well supported on win32) and processes (not well supported on gtk/unix) additional functions have been added:

TChildExitReason = (cerExit, cerSignal);
TPipeReason = (prDataAvailable, prBroken, prCanWrite);
TPipeReasons = set of TPipeReason;

TChildExitEvent = procedure(AData: PtrInt; AReason: TChildExitReason; AInfo: dword) of object;
TPipeEvent = procedure(AData: PtrInt; AReasons: TPipeReasons) of object;
function  AddProcessEventHandler(AHandle: THandle; 
    AEventHandler: TChildExitEvent; AData: PtrInt): PProcessEventHandler;
procedure RemoveProcessEventHandler(AHandler: PProcessEventHandler);

function  AddPipeEventHandler(AHandle: THandle; 
    AEventHandler: TPipeEvent; AData: PtrInt): PPipeEventHandler;
procedure RemovePipeEventHandler(AHandler: PPipeEventHandler);

When a process terminates the event handler specified will be called. AInfo will contain the exit code if AReason is cerExit, or (on unix only) the termination signal if AReason is cerSignal. For gtk/unix, use the PID to watch as AHandle. Internally, a signal handler is installed to catch the SIGCHLD signal. On win32, AddEventHandler is used to watch process termination.

To watch a pipe for incoming data, pass the file descriptor (unix) or the pipe read-end handle to AddPipeEventHandler with a custom chosen event handler method as AEventHandler. On gtk/unix, AddEventHandler is called to watch the file descriptor for traffic. On win32, the message loop will timeout every 100 msecs to check all watched pipes for available data; if data is available, the event handler is called.