开发者

Holding scrollbar freezes win32 console application

开发者 https://www.devze.com 2023-04-02 07:37 出处:网络
I wrote this little program to illustrate my problem : int main(int argc, char* argv[]) { int i = 0; while(1)

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消