I wrote this little program to illustrate my problem :
int main(int argc, char* argv[])
{
int i = 0;
while(1)
{
std::cout << i++ << std::endl;
Sleep(1000);
}
return 0;
}
So this simple program will stop counting if you hold 开发者_StackOverflow社区the vertical scroll bar ( to watch back logs or whatever ...).
Is there a way to avoid this ?
Cheers
Not really. What happens is that holding the scrollbar prevents the application to write any new output to the console, so it eventually blocks on flushing std::cout. This is due to how Windows implements the console and can not be avoided.
If you can't rely on the continued execution of the program, you could instead calculate i
based on the time elapsed from the initial program:
#include <ctime>
time_t initialSeconds;
int main(int argc, char* argv[])
{
double i=0;
//Initialise time
initialSeconds = time (NULL);
while(1) {
i = difftime(initialSeconds, time(NULL));
sleep(1000);
}
}
This will calculate the number of seconds elapsed based on the computers clock.
(Haven't tested this as I'm not on a machine with a compiler)
精彩评论