[Haskell-cafe] Windows: how to perform cleanup actions when the console window closes

Peter Dedecker nectarien at gmail.com
Fri Apr 6 09:35:57 UTC 2018

Hi Joachim,

> You need to intercept the various events that may cause a console
> windows to be closed; this is done via the Windows API call
> SetConsoleCtrlHandler .
> https://stackoverflow.com/a/30843219/6944068 gives an overview

very informative, thanks!

> I don't know how to register a Haskell function as a Windows callback,
> that will have to be answered by somebody else.

With your tips, I came across
(The documentation doesn't work, but you can look at the source to see
the gist of it). I think this may do what you describe.

The documentation for this function says that
> When the event is received, if the
> 'Handler' value is @Catch f@, then a new thread will be spawned by
> the system to execute @f e@, where @e@ is the 'ConsoleEvent' that
> was received.

I wonder if I can add a handler function that detects Control-C and
Close events, and responds by throwing a userInterrupt exception to
the main thread, and then sleeping forever. The main thread then sees
an asynchronous exception, the bracket cleanup action fires (which is
my goal here), main thread shuts down and the program stops. Will try
to experiment with that later.

Thanks again

More information about the Haskell-Cafe mailing list