Computing.Net > Forums > Programming > Wait State C Programming

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Wait State C Programming

Reply to Message Icon

Name: Grune
Date: February 20, 2002 at 13:46:38 Pacific
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Tom
Date: February 20, 2002 at 14:53:25 Pacific
Reply:

Yeah, sleep(int). I think the argument is the amount of miliseconds you want it to sleep for.


0

Response Number 2
Name: Grune
Date: February 20, 2002 at 15:30:11 Pacific
Reply:

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


0

Response Number 3
Name: computers-are-cool (by Luke)
Date: February 21, 2002 at 09:42:34 Pacific
Reply:

Hey,

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


0

Response Number 4
Name: Kenneth
Date: February 22, 2002 at 07:26:36 Pacific
Reply:

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
}
}


0

Response Number 5
Name: Kenneth
Date: February 22, 2002 at 07:28:18 Pacific
Reply:

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.


0

Related Posts

See More



Response Number 6
Name: Grune
Date: February 23, 2002 at 12:13:53 Pacific
Reply:

Thanks! Why wouldn't I want to use this method? It looks like it would be fine.


0

Response Number 7
Name: Colin
Date: June 6, 2002 at 15:12:13 Pacific
Reply:

try Sleep(DWORD milliseconds)

note the capital 'S' in Sleep


0

Response Number 8
Name: Valdrax
Date: July 12, 2002 at 13:08:15 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Wait State C Programming

C Programming Problem , www.computing.net/answers/programming/c-programming-problem-/942.html

c++ programming for novell www.computing.net/answers/programming/c-programming-for-novell/2525.html

Bike Trader C program www.computing.net/answers/programming/bike-trader-c-program/6557.html