Help us understand the problem. What is going on with this article?

System.FSNotifyでファイルイベントが一切検出されない時

解決策: withManagerを実行しているスレッド自体を起動させ続ける

haddockの例プログラムのコメントに

-- start a watching job (in the background)

と書かれているので「これ自身でforkIOしてくれている」と思ってしまいがちだ(思ってしまった)が、
それを実行している側のスレッドも生かしておく必要があるらしい。
自分の場合、メインスレッドでは他のプログラムを動かしていたので、forever $ threadDelay 10000を抜かしてしまいこれにはまった。
なので、同じようにメインスレッドは別でやることがある場合以下のようにforkIOしてあげるといい。

watchUpdate ... = forkIO . withManager . \mng -> do
    -- `watchDir`等で監視する
    forever $ threadDelay 10000

ちなみに、自分が使った例はこちら(github: Cj-bc/brick-shgif)

-- | Watch specified file and send FileUpdated event
-- when it's updated.
watchUpdate :: BChan CustomEvent -> FilePath -> IO ()
watchUpdate ch fn = void . forkIO . withManager $ \mng -> do
    watchDir
        mng
        (takeDirectory fn)
        isWatchedFile
        sendEvent
    forever $ threadDelay 10000
    where
        isWatchedFile :: FSN.Event -> Bool
        isWatchedFile e = case e of
                 Modified fn _ _ -> True
                 otherwise       -> False

        sendEvent :: FSN.Event -> IO ()
        sendEvent = \_ -> writeBChan ch FileUpdated
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした