Skip to content

Instantly share code, notes, and snippets.

@kakkun61
Last active January 30, 2019 14:43
Show Gist options
  • Save kakkun61/c131ab38dc3f1bdc65f51ef415d4797d to your computer and use it in GitHub Desktop.
Save kakkun61/c131ab38dc3f1bdc65f51ef415d4797d to your computer and use it in GitHub Desktop.
Ctrl-C with Haskell on Windows
import Control.Concurrent
import Control.Monad
import System.Exit
import System.IO
import System.Win32.Console.CtrlHandler
main :: IO ()
main = do
tid <- myThreadId
let
handler event = do
if event == cTRL_C_EVENT
then do
putStrLn "goodbye!"
killThread tid
pure True
else
pure False
pHandler <- mkHandler handler
success <- c_SetConsoleCtrlHandler pHandler True
when (not success) $ do
putStrLn "SetConsoleCtrlHandler failed"
exitFailure
let
loop n = do
putStr $ show n ++ ", "
hFlush stdout
threadDelay 1000000
loop (n+1)
loop 0
name: windows-interruption
dependencies:
- base >= 4.7 && < 5
- Win32
executables:
app-rtsopts-threaded:
main: Main.hs
source-dirs: .
ghc-options:
- -rtsopts
- -threaded
app-threaded:
main: Main.hs
source-dirs: .
ghc-options:
- -threaded
app-rtsopts:
main: Main.hs
source-dirs: .
ghc-options:
- -rtsopts
app:
main: Main.hs
source-dirs: .
resolver: lts-12.26
packages:
- .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment