Skip to content

The best way to show a timer while processing something asynchronously? #486

Closed Answered by amano-kenji
amano-kenji asked this question in Q&A
Discussion options

You must be logged in to vote

I found a solution.

now <- getCurrentTime
stopTimer <- newEmptyTMVarIO
timerStopped <- newEmptyTMVarIO
let calcText old new = T.unlines
      [ "Calculating statistics..."
      , "If you want to stop this, kill this program"
      , "Elapsed time: " <> T.pack (formatTime defaultTimeLocale "%2h:%2M:%2S" $ diffUTCTime new old) ]
    timer = do
      now' <- getCurrentTime
      writeBChan respC $ AsyncEvent $ modify $ set #mode $ Processing $ calcText now now'
      threadDelay 1000000
      stop <- atomically $ tryTakeTMVar stopTimer
      case stop of
        Nothing -> timer
        Just () -> atomically $ putTMVar timerStopped ()
void $ forkIO timer
do the work that takes a long time
p…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by amano-kenji
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants