Windows: how to perform cleanup actions when the console window closes

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Windows: how to perform cleanup actions when the console window closes

Peter Dedecker
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
https://hackage.haskell.org/package/base-4.11.0.0/docs/GHC-ConsoleHandler.html.
(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
Peter
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.