WaitForSingleObject() function
Aug 14, 2014 at 10:40am UTC
The following program loops if the console that it is running on is in focus.
But the program stops soon as the console is out of focus.
Is there a way to keep the program running when the console is out of focus?
The WaitForSingleObject function page is on
http://msdn.microsoft.com/en-us/library/windows/desktop/ms687032(v=vs.85).aspx
I have no experience with multi-threading, so I don't under stand much of the WaitForSingleObject page.
I am running MinGW compiler Windows 7.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <iostream>
#include <stdio.h>
#include <windows.h>
int main()
{
unsigned long int count=0; // 4,294,967,295 max
char c;
while (true )
{
if (WaitForSingleObject( GetStdHandle( STD_INPUT_HANDLE ), 0) == WAIT_OBJECT_0)
//if (false) //loops even when console is not in focus
{
c = getchar();
if (c == 'q' ) //quit
{
break ;
}
if (c == 'c' ) //continue
{
std::cout << "Enter 'c' to continue, 'q' to quit: " << std::flush;
}
}
//do stuff
std::cout << count++ << " " << std::flush;
}
return 0;
}
When I replace the "if (WaitForSingleObject)" on line 12 with "if (false)", the program keeps looping, no matter what the focus.
Thank you.
Topic archived. No new replies allowed.