Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7411857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:27:06+00:00 2026-05-29T06:27:06+00:00

How does one block until the earlier of (1) a keypress or (2) a

  • 0

How does one block until the earlier of (1) a keypress or (2) a previously input time of day in hh:mm format is reached. I am using Windows in case that matters. This DOS assembler program (which does run on Windows too) does what I want via something like batchman waittil 16:30 from the Windows console but I want to do it entirely in Haskell, (i.e. without making use of that program).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:27:07+00:00Added an answer on May 29, 2026 at 6:27 am

    You can start two threads: one reads a character, the other waits until the specified time is reached; they both write to a single MVar to signal completion.

    This is a little tricky, but mostly due to the details: we want to have stdin in unbuffered and non-echoing mode so that a single keypress stops the waiting without printing anything, and then restore the original state afterwards; and we also need to kill both threads after either finishes, so that we, for example, stop reading from stdin once the timeout expires. Additionally, we need to ensure things are cleaned up properly if an exception occurs. bracket simplifies the clean-up logic here, but it’s still pretty ugly:

    import Prelude hiding (catch)
    import Control.Exception
    import Control.Concurrent
    import System.IO
    
    withRawStdin :: IO a -> IO a
    withRawStdin = bracket uncook restore . const
      where
        uncook = do
            oldBuffering <- hGetBuffering stdin
            oldEcho <- hGetEcho stdin
            hSetBuffering stdin NoBuffering
            hSetEcho stdin False
            return (oldBuffering, oldEcho)
        restore (oldBuffering, oldEcho) = do
            hSetBuffering stdin oldBuffering
            hSetEcho stdin oldEcho
    
    waitFor :: Int -> IO ()
    waitFor delay = do
        done <- newEmptyMVar
        withRawStdin . bracket (start done) cleanUp $ \_ -> takeMVar done
      where
        start done = do
            t1 <- forkIO $ getChar >> putMVar done ()
            t2 <- forkIO $ threadDelay delay >> putMVar done ()
            return (t1, t2)
        cleanUp (t1, t2) = do
            killThread t1
            killThread t2
    

    Even after all that, this solution still doesn’t handle waiting until a specific time — just waiting a certain number of microseconds. For turning a time of day into a number of microseconds to sleep, this previous SO question may help. If the sleeps are sufficiently long, then they might not fit into an Int of microseconds, so you might have to use threadDelay in a loop, or delay from the unbounded-delays package.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How does one go about catching exceptions from using controls in markup? For example,
How does one set a timeout on a BufferedReader and a PrintWriter created using
Using the lock statement , one can ensure that one thread does not enter
does one perform better over the other in terms of indexing/quering etc ? e.g.
How does one test a method that does some interactions with the local D-Bus
How does one compare single character strings in Perl? Right now, I'm tryin to
How does one convert from an int or a decimal to a float in
How does one obtain programmatically a reference to the object of which a FieldInfo
How does one choose if someone justify their design tradeoffs in terms of optimised
How does one find out which controller or which action they are in from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.