CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Waiting Forever

  1. #1
    Join Date
    Jul 2004
    Posts
    1

    Waiting Forever

    I have an application where I am entering a while loop and looking at 2 things. The first is if a triggered event has occurred and the second is a user input to stop the loop. I have looked at several different things and can't find a good solution. I thought about using a thread but then if there is never a user input the thread will continue to run. Eventually, I want to send commands in using a socket but right now I am just trying to set everything up on the local machine. Is there any other way to check a stream (be it file or cin) with out pausing the overall operation of the application?

  2. #2
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664
    There is no such function in C that allows you to "poll" or ask for status on a stream (FILE *), without blocking. I don't think there is one in C++ either (but I'm not sure about the standard C++ library). You'll most likely need to use some OS specific API function for this.

    In Windows you have many options. You can poll the keyboard using _kbhit(). You can poll network events with the socket function select().
    None of these are standard C and C++ functions.

  3. #3
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773
    select is a Posix function (I think).

  4. #4
    Join Date
    Jun 2004
    Location
    India
    Posts
    432
    _kbhit appears to be a C run time lib function. And select() is a Berkley sockets API, with the function being provided in almost all socket implementations on various platforms. So you can safely use any of them in your programs.

    So in a loop, wait for a event with a timeout, the wait will either be satisfied or timeout. If wait is satisfied, do what you want to do. On timeout poll for keyboard/socket through _kbhit/select, if the poll indicates some input is there, process it, else loop.
    Say no to supplying ready made code for homework/work assignments!!

    Please rate this post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured