Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I need a little pause in my program, but an automatic pause. I have some thing that says:
printf("Loading diag...");
I would like a little pause in my program at this point. Is there a function that causes the program to pause for a few seconds. I was using a for() loop, but the wait state will depend on the speed of each individuals computer.Thanks

I tried sleep but the compiler didn't recognise it. I am using MS Visual C++. Is there a special header I must use? Thanks

Hey,
Try stdio.h and if that doesn't work windows.h... It's been a while for me since I used that function.

Although you might not want to use this method, it certainly would work. (i think.)
Include windows.h in your program. You can use the function GetTickCount() to find out how many milliseconds windows has been running for. So, say, if you want to make a generic pause routine:
#include
void pause(int msec)
{
long starttime;
starttime = GetTickCount();
while (GetTickCount - starttime < msec)
{
// do nothing
}
}

For some reason, the formatting in the code above did not come out right. The #include includes windows.h, and GetTickCount should have parens () at the end inside the while test. And there should be indenting. Argh.
That is all.

You would not want to use the GetTickCount() method he described because it is wasteful. You program will be taking up CPU cycles while looping and making that comparison until it evaluates false. Since this is a very tight loop, it will max out your CPU while running. This is pretty much the opposite of taking a pause for a couple of seconds.
Sleep() will actually take your process off the process queue and put in a "wait" state until the time has elapsed. Therefore, your program will not be wasting system resources until it is needed again. When the time has elapsed, it will put your program back in line to run again. Sleep() is the Windows equivalent of the UNIX sleep() and usleep() functions which put the process to sleep for an int number of seconds or microseconds, respectively.
There is more advanced functionality available in Windows for putting a process to sleep, but it sounds like your code doesn't need anything fancy.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |