Ctrl-Break handler
Factor handbook » The implementation » System interface

Prev:Operating system detection
Next:vm-path ( -- path )


There is one global handler available per Factor VM, disabled by default. When enabled, it starts a separate native thread and polls the keyboard for the Ctrl-Break combination. If user presses Ctrl-Break with one of the Factor windows active, the handler causes an exception to be thrown in the main thread, which allows user to interrupt a VM stuck in a busy loop.

Due to specific implementation requirements, this facility is only available on Windows platforms. Namely, it needs the ability to poll the keyboard state while the input focus belongs to another thread.

While the handler is active, it can interrupt any code in Factor VM, including sensitive or low-level functions. If this happens, chances are VM won't be able to recover. To prevent crashes, only enable the handler while user code is running in the foreground. Don't enable it in the background threads before yielding, don't have it enabled while GC is working, etc. Always make sure you can catch the exception it would produce.

The listener can activate the Ctrl-Break handler while it's compiling and running user code interactively, so that user could interrupt an infinite loop. To allow the listener use this facility, add the following code to your Startup initialization file:
USING: listener namespaces ; t handle-ctrl-break set-global


Managing the Ctrl-Break handler:
enable-ctrl-break ( -- )

disable-ctrl-break ( -- )