Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I wait x number of milliseconds??

Status
Not open for further replies.

Oxymoron

Technical User
Dec 17, 2000
168
0
0
GB
Hi.

Using just an iterative loop I want to wait for say 400 ms.
I dont want to use the sleep program, but if there are any other C functions or a way of figuring out how many iterations make up a millisecond that'd be great!

Any and all suggestions welcome.
Many thanks, jOe

we are all of us living in the gutter.
But some of us are looking at the stars.
 
Windows API: void Sleep(unsigned long mswait);
Alas, no such portable C function...
 
There is a POSIX function called nanosleep but not all implementations have it. It doesn't exist in Windows. It exists in some implementations of Unix but not all.

So much for POSIX.
 
You can use select(). I'm no expert, but i think it exists in all implementations.

Theophilos.

-----------

There are only 10 kinds of people: Those who understand binary and those who don't.
 
try this function:

int suspend(int milisecs)
{


HANDLE hTimer = NULL;
LARGE_INTEGER liDueTime;

liDueTime.QuadPart=(-10000 * milisecs);

// Create a waitable timer.
hTimer = CreateWaitableTimer(NULL, TRUE, NULL);
if (!hTimer)
{
CloseHandle(hTimer);
return 1; // error creating timer
}


// Set a timer to wait for milisec miliseconds.
if (!SetWaitableTimer(hTimer, &liDueTime, 0, NULL, NULL, 0))
{
CloseHandle(hTimer);
return 2; // error setting timer
}

// Wait for the timer.

if (WaitForSingleObject(hTimer, (milisecs+10)) != WAIT_OBJECT_0)
{
CloseHandle(hTimer);
return 3; // failed
}

CloseHandle(hTimer);
return 0; // success
 
>>I’m no expert, but doesn’t delay(500); work?
Only if your compiler supports it. It's not a standard function.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top