我是Haskell的全新用戶,並在一些示例中弄亂了我遇到的問題,無法停止該程序。我正在使用Windows 7並使用ght的runhaskell。 Ctrl-c不起作用,所以我不得不訴諸任務管理器,這有點痛苦。控制線程退出haskell應用程序
而不是如何創建一個單獨的控制線程,直到我鍵入q然後退出我的Haskell應用程序。
我已經得到了問題的應用程序的格式爲:
main = do
h <- connectTo server (PortNumber (fromInteger port))
hSetBuffering h NoBuffering
... do some stuff with the socket handle ...
listen h
listen :: Handle -> IO()
listen h = forever $ do
t <- hGetLine h
let s = init t
putStrLn s
where
forever a = do a; forever a
在僞代碼想什麼,我已經是:
main = do
waitForQuit
... original program ...
waitForQuit :: IO()
option <- getChar
if option == 'q' then
... kill the app ...
else
waitForQuit
程序僅在主線程退出時退出,因此您需要交換'...'和'exitOnQ'。在額外的線程中執行工作,主要是'exitWith''。 – barsoap 2012-10-15 15:38:47