Click to See Complete Forum and Search --> : Waiting Forever
RWoods
July 21st, 2004, 01:56 PM
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?
j0nas
July 21st, 2004, 04:08 PM
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.
NMTop40
July 21st, 2004, 04:35 PM
select is a Posix function (I think).
UnderDog
July 22nd, 2004, 01:54 AM
_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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.