Sign in to follow this  
Sagar_Indurkhya

Pausing a program

Recommended Posts

I've written a scientific simulation program in C++. The program operates by just going again and again through a loop simulating some reactions, but it takes days or even weeks sometimes to run. So I want a way to pause the simulation by hitting any key at the keyboard. At each iteration I want a way to check if the user has entered anything, and if not to continue on. Most of the input methods I'm familiar with wait for the user to enter something before moving on. Can someone point me in the right direction?

Share this post


Link to post
Share on other sites
Two simple methods of using nonblocking inputs are by using threads or, if in Windows, GetAsyncKeyState( ). If going with the multithreaded approach, just spawn a key listening thread that doesn't do anything until a key is pressed, then pauses the main thread. The second method is probably simpler, but you're tied to Windows and not every key will cause the pause (heh); there may be equivalent functions on other OSes, though. Here's a sample of it in use:
void FunctionThatDoesStuff( )
{
// the high-order bit of the return of GetAsyncKeyState determines whether
// the key is down or not; if the high bit is set (key is down),
// the return is negative
if (GetAsyncKeyState(VK_SPACE) < 0)
bPaused = !bPaused;

if (!bPaused)
{
// do stuff
}
}


Either of those do the trick?

Share this post


Link to post
Share on other sites
Presumably you've written your program as a command-line program. If you revamp it as a Windows program, Windows will queue events for you, and you can check for the keyboard events (WM_CHAR or WM_KEYDOWN I believe) in between each loop.

Sadly if you haven't already done this there's probably a bit of a learning curve involved. Also if cross platform matters, this is obviously non-helpful, though there are similar approaches on any platform you'll need.

Hope this helps,
Geoff

Share this post


Link to post
Share on other sites
If you really must do it with standard stuff, you could try:

1) Replace the streambuf of std::cin with one which does not do line buffering. Then use cin.peek() to see if there is anything in the buffer; if there is, .get() the character and toggle the program-paused state.

2) Try to disable the line buffering with the cstdlib function setvbuf(). You might also have to cin.sync_with_stdio(). Then proceed as before with peek and get.

Share this post


Link to post
Share on other sites
Quote:
Original post by gdunbar
Presumably you've written your program as a command-line program. If you revamp it as a Windows program, Windows will queue events for you, and you can check for the keyboard events (WM_CHAR or WM_KEYDOWN I believe) in between each loop.

Sadly if you haven't already done this there's probably a bit of a learning curve involved. Also if cross platform matters, this is obviously non-helpful, though there are similar approaches on any platform you'll need.

Hope this helps,
Geoff


A message queue can be created in a console program without need for a window.


// create the message queue
MSG msg;
DWORD ThreadID;
ThreadID = GetCurrentThreadId();
PeekMessage(&msg, NULL, 0, 0,PM_NOREMOVE);
// don't continue until the queue is created
PostThreadMessage(ThreadID, WM_NULL, 0, 0);
WaitMessage();
// message pump & idle processing



See Multithreading Part III: User-Interface Threads

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this