Pausing a program

Started by
4 comments, last by KulSeran 17 years, 1 month ago
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?
Advertisement
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?
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
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.
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
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
_kbhit() also works to tell you if there is a char in the buffer,
then use getch() to check what that char is.

This topic is closed to new replies.

Advertisement