Enoncé du problème
Vous devez attendre un événement (sur le sockethttp://www.dicofr.com/cgi-bin/n.pl/dicofr/definition/20010101004807 ou pipe ou ...) qui arrive, mais vous voulez faire cela dans le processus principal (GUI) et vous ne voulez pas bloquer le GUI et aussi vous ne voulez pas faire du multi-threading. La solution à ce problème est la possibilité d'ajouter des "handles" http://www.dicofr.com/cgi-bin/n.pl/dicofr/definition/20010101002342 supplémentaires pour être observés dans la boucle d'événement principale.
Les détails de la Solution
Dans l'unité LCLIntf deux fonctions ont été ajoutées pour tenir compte de cette fonctionnalité, ce sont :
AddEventHandler(AHandle: THandle; AFlags: dword;
AEventHandler: TWaitHandleEvent; AData: PtrInt): PEventHandler;
RemoveEventHandler(var AHandler: PEventHandler);
SetEventHandlerFlags(AHandler: PEventHandler; NewFlags: dword);
Le TWaitHandleEvent est déclaré dans l'unité InterfaceBase comme :
TWaitHandleEvent = procedure(AData: PtrInt; AFlags: dword) of object;
AddEventHandler ajoute un handle pour être observé dans la boucle d'évênement principale. Quand le handle est "signalé ", alors la procédure indiqué dans ACallback sera appellé avec comme paramètre AData qui était passé lors de l'enregistrement du traiteur d'évênement, et tous drapeaux , qui dont spécifiques au système d'exploitation. Le drapeau passés à AddEventHandler peut être modifié en utilisants le SetEventHandlerFlags. Le handler retourné (un pointeur) par AddEventHandler devrait être passé à RemoveEventHandler pour cesser d'observer l'handle associée .
RemoveEventHandler arrête d'observer l'handle spécifié. RemoveEventHandler fixera AHandler à nil avant le renvoi . Son argument est un pointeur retourné près AddEventHandler.
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(var AHandler: PProcessEventHandler);
function AddPipeEventHandler(AHandle: THandle;
AEventHandler: TPipeEvent; AData: PtrInt): PPipeEventHandler;
procedure RemovePipeEventHandler(var 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.