English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
OCaml IDE (Camlp4 for code formatting)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-06-29 (16:13)
From: Christopher A. Watford <christopher.watford@g...>
Subject: Re: [Caml-list] Keyboard interrupt in Windows
On 6/29/05, Nathaniel J. Gaylinn <ngaylinn@cs.brown.edu> wrote:
> In Linux, OCaml uses signals to break out of the current evaluation (when
> you press Ctrl+C to cut out of an infinite loop). However, Windows doesn't
> support signals. What does OCaml do differently under Windows to make this
> work?
>  -- Nate Gaylinn

Win32 DOES support signals, it just does not send any signal on
CTRL+C. A second thread opens that posts a message to WinMain. The
OCaml Windows IDE has an example of using this to send the interrupt
to the toplevel.

In the following file at the very bottom:

// The following sends a CTRL+C/CTRL+BREAK to the console.
GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, pi.dwProcessId)

Note you MUST write to the Win32 Pipe Handle AFTER you call the break,
otherwise your application will have no idea the pipe was interrupted.

As far as how the toplevel itself handles the CTRL_BREAK_EVENT:

// PHANDLER_ROUTINE looks like: BOOL WINAPI HandlerRoutine(DWORD dwCtrlType);
// Add - TRUE to add a handler, FALSE to remove the handler
BOOL SetConsoleCtrlHandler(PHANDLER_ROUTINE HandlerRoutine, BOOL Add);

And the CTRL_*_EVENTs you can handle are:
CTRL_CLOSE_EVENT - [X] clicked
CTRL_LOGOFF_EVENT - user logoff
CTRL_SHUTDOWN_EVENT - machine shutdown or service shutdown

Hope that helps.

Christopher A. Watford