作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试了以下方法:
import System.Exit
import System.Posix.Signals
import Control.Concurrent (threadDelay)
main :: IO ()
main = do
installHandler keyboardSignal (Catch (do exitSuccess)) Nothing
threadDelay (1000000000)
^CTest.hs: ExitSuccess
Ctrl-C
,而不是退出。我应该如何正确地做到这一点?
最佳答案
来自 installHandler
的文档:
a handler is installed which will invoke action in a new thread when (or shortly after) the signal is received.
exitWith:
Note: in GHC, exitWith should be called from the main program thread in order to exit the process. When called from another thread, exitWith will throw an ExitException as normal, but the exception will not cause the process itself to exit.
exitSuccess
处理程序不会结束该过程,这是预期的(尽管是意外的;)行为。
import System.Exit
import System.Posix.Signals
import Control.Concurrent
main :: IO ()
main = do
tid <- myThreadId
installHandler keyboardSignal (Catch (killThread tid)) Nothing
threadDelay (1000000000)
import System.Exit
import System.Posix.Signals
import Control.Concurrent
import qualified Control.Exception as E
main :: IO ()
main = do
tid <- myThreadId
installHandler keyboardSignal (Catch (E.throwTo tid ExitSuccess)) Nothing
threadDelay (10000000)
关于haskell - 如何在 Haskell 中编写 Ctrl-C 处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13441676/
我是一名优秀的程序员,十分优秀!