Click to See Complete Forum and Search --> : Wait for input, if none continue


howardnl
May 13th, 2002, 08:32 AM
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

jparsons
May 14th, 2002, 09:31 AM
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

howardnl
May 14th, 2002, 10:44 AM
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

jparsons
May 14th, 2002, 11:37 AM
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

kuphryn
May 14th, 2002, 06:41 PM
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

Stitch
May 15th, 2002, 08:06 AM
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
====================

jparsons
May 15th, 2002, 09:17 AM
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

kuphryn
May 15th, 2002, 09:37 AM
Multithreading is fun and features flexibility. Under multithreading, you get to design the program the way you exactly how you want it.

Kuphryn