Ctrl-C with Haskell on Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resolver: lts-12.26 | |
packages: | |
- . |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment