Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Taking input while in some form of waiting/sleeping -- plz help

Taking input while in some form of waiting/sleeping -- plz help

Scheduled Pinned Locked Moved Managed C++/CLI
c++helpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Makutu
    wrote on last edited by
    #1

    The subject basically says the gist of it. I'm trying to write a program in C++ where it waits for input for up to 2 seconds but if nothing happens in 2 seconds, it stops looking for input and goes off to do something else. I know this may touch on stuff that you need multi-threading for but I think I remember reading somewhere that there's a simpler way for something as simple as I'm looking to do. Does anyone have any code snippets or any ideas? Thanks in advance for your help. MaKuTu t3h n00b

    C 1 Reply Last reply
    0
    • M Makutu

      The subject basically says the gist of it. I'm trying to write a program in C++ where it waits for input for up to 2 seconds but if nothing happens in 2 seconds, it stops looking for input and goes off to do something else. I know this may touch on stuff that you need multi-threading for but I think I remember reading somewhere that there's a simpler way for something as simple as I'm looking to do. Does anyone have any code snippets or any ideas? Thanks in advance for your help. MaKuTu t3h n00b

      C Offline
      C Offline
      Curi0us_George
      wrote on last edited by
      #2

      Probably more elegant to do this in a multithreaded fashion, but yes, it's possible to do what you want without multithreading. First, you're going to need non-blocking input. If you don't have that, I think you're pretty much SOL. Assuming you have non-blocking input, the basic logic is:

      int granularity = 100; // number of milliseconds to sleep between attempts
      int attempt = 2000; // number of milliseconds to try
      int attempted = 0; // counter
      while (attempted < attempt)
      {
      // check for input (must not block)
      if (inputIsAvailable())
      {
      // we already have input
      doSomething();
      }
      else
      {
      // wait for input
      Sleep(granularity);
      attempted += granularity;
      }
      }
      // we never got input
      doSomethingElse();

      M 1 Reply Last reply
      0
      • C Curi0us_George

        Probably more elegant to do this in a multithreaded fashion, but yes, it's possible to do what you want without multithreading. First, you're going to need non-blocking input. If you don't have that, I think you're pretty much SOL. Assuming you have non-blocking input, the basic logic is:

        int granularity = 100; // number of milliseconds to sleep between attempts
        int attempt = 2000; // number of milliseconds to try
        int attempted = 0; // counter
        while (attempted < attempt)
        {
        // check for input (must not block)
        if (inputIsAvailable())
        {
        // we already have input
        doSomething();
        }
        else
        {
        // wait for input
        Sleep(granularity);
        attempted += granularity;
        }
        }
        // we never got input
        doSomethingElse();

        M Offline
        M Offline
        Makutu
        wrote on last edited by
        #3

        I guess I don't know whether or not I have non-blocking input. Do you know any examples of what a function like that might be called/what header file you need? Thx

        C 1 Reply Last reply
        0
        • M Makutu

          I guess I don't know whether or not I have non-blocking input. Do you know any examples of what a function like that might be called/what header file you need? Thx

          C Offline
          C Offline
          Curi0us_George
          wrote on last edited by
          #4

          Not really. That would be entirely dependent on what libraries you ae using for input. Perhaps whatever library you are using provides a function (with a name such as bytes_available()) which returns the number of bytes currently available for access. I've used libraries (e.g. JNetLib) which has this type of functionality, but many libraries do not. It is possible to wrap an asynchronous APi and achieve this kind of functionality (which is what JNetLib does), but this wrapping will require threading. It really depends on what you are trying to do, where this input is coming from, and what libraries you are using.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups