|
-
May 13th, 2002, 08:32 AM
#1
Wait for input, if none continue
Hello,
I've written a simple console app that will run for a very long time if uninterrupted. My program does some work, delays, more work, another delay. There is currently no user interaction through the console app, it just cranks away at data. How can I let the user enter something, but if they don't continue on?
Thanks,
Nick
-
May 14th, 2002, 09:31 AM
#2
Re: Wait for input, if none continue
Are you programing in Windows or Unix/Linux? If you're coding in Linux/Unix environment use the alarm() function. This will interrupt your reading call after a set time and you may continue on in your code.
Jared Parsons
Jared
-
May 14th, 2002, 10:44 AM
#3
Re: Wait for input, if none continue
Thanks for the help Jared. I'm using Windows. Do you know if there is a similar function for Windows? Thanks, I appreciate your help.
Thanks,
Nick
-
May 14th, 2002, 11:37 AM
#4
Re: Wait for input, if none continue
I know there is a way to do it in windows but I'm having trouble finding it. You could do a nasty mutli threading hack but there should be an easier way to do it.
Jared Parsons
Jared
-
May 14th, 2002, 06:41 PM
#5
Re: Wait for input, if none continue
One solution is to use a variable as an updater. For example:
// bool m_bUserActivity = false;
In your worker thread:
// while (!m_bUserActivity) // no activity
//{
//...
//}
If there is user activity (key stroke, mouse, etc.):
// m_bUserActivity = true;
Once that happens, you should close the worker thread.
Kuphryn
-
May 15th, 2002, 08:06 AM
#6
Re: Wait for input, if none continue
Have a look at <conio.h> and the functions kbhit() and getch(). Test
kbhit for weather there is input waiting in the buffer, getch waits
or a character if one is not present.
This will only work in a console app
Stitch
====================
-
May 15th, 2002, 09:17 AM
#7
Re: Wait for input, if none continue
I was looking to solve it without mutliple threads. Too bad Windows doesn't have interrupts. Having to create another thread to make sure an input call won't block is not the best programming model IMHO.
Jared Parsons
Jared
-
May 15th, 2002, 09:37 AM
#8
Re: Wait for input, if none continue
Multithreading is fun and features flexibility. Under multithreading, you get to design the program the way you exactly how you want it.
Kuphryn
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|