GreatOne
asked on
Wait/Pause function in C?
Is there a function in C that will pause for a specified amount of time?
If so, what library must I #include?
Thanks
If so, what library must I #include?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
He did not say which system he uses the VC online help says:
standard dos compiler do not have the sleep function
remember msc 5.1 or 6.0 ?
Sleep
Windows NT Yes
Win95 Yes
Win32s Yes
Import Library kernel32.lib
Header File winbase.h
Unicode No
Platform Notes None
this function works always:
void wait(time_t TimeToWait)
{
time_t StartTime=time(NULL);
time_t ActualTime;
do
{
ActualTime=time(NULL);
if((StatTime+TimeToWait)>= ActualTime )
break;
if(ActualTime<StartTime) //Midnight crossing
StartTime=ActualTime;//thi s is not a good solution but it prevents the loop running endless
}while(TRUE)
}
standard dos compiler do not have the sleep function
remember msc 5.1 or 6.0 ?
Sleep
Windows NT Yes
Win95 Yes
Win32s Yes
Import Library kernel32.lib
Header File winbase.h
Unicode No
Platform Notes None
this function works always:
void wait(time_t TimeToWait)
{
time_t StartTime=time(NULL);
time_t ActualTime;
do
{
ActualTime=time(NULL);
if((StatTime+TimeToWait)>=
break;
if(ActualTime<StartTime) //Midnight crossing
StartTime=ActualTime;//thi
}while(TRUE)
}
I forgot
#include <time.h>
#include <time.h>
ASKER
I am using a DOS compiler. Norbert, I tried using your wait function but it won't compile. I get a message that it doesn't know what TRUE is. Also, how would i call wait? do i just call it as wait(number of secs I want to wait) or another way?
Ok If True is not defined make while(1)
the resolution of the time function is second
therefore you will call for a 2 sec wait
wait(2)
If you like my answer and it works for you please reopen the question
if you need a higher resolution than seconds here a trick:
WaitHighRes(unsigned long TimeToWait)
{
volatile long *Biostime=(long*)0x0000046 c; //adress of bios timer using dos
unsigned long Starttime=*Biostime;
unsigned long time_t ActualTime;
do
{
ActualTime=*Bi ostime;
if((StatTime+T imeToWait) >=ActualTi me)
break;
if(ActualTime <StartTime ) //Midnight crossing
StartTime =ActualTim e;//this is not a good solution but it prevents the loop running endless
}while(1)
}
now you have a resolution of 1/18.2 s
that means you can call
WaitHighRes(18) for waiting 1 second
Hope that helps
Norbert
the resolution of the time function is second
therefore you will call for a 2 sec wait
wait(2)
If you like my answer and it works for you please reopen the question
if you need a higher resolution than seconds here a trick:
WaitHighRes(unsigned long TimeToWait)
{
volatile long *Biostime=(long*)0x0000046
unsigned long Starttime=*Biostime;
unsigned long time_t ActualTime;
do
{
ActualTime=*Bi
if((StatTime+T
break;
if(ActualTime
StartTime
}while(1)
}
now you have a resolution of 1/18.2 s
that means you can call
WaitHighRes(18) for waiting 1 second
Hope that helps
Norbert
for security reasons to not depend on model small, medium compact, large or huge change the line
volatile long *Biostime=(long*)0x0000046 c; //adress of bios timer using dos
to
volatile long far*Biostime=(long far*)0x0000046c; //adress of bios timer using dos
volatile long *Biostime=(long*)0x0000046
to
volatile long far*Biostime=(long far*)0x0000046c; //adress of bios timer using dos
ASKER
Norton,
I made the changes and now it compiles but it does not pause at all. I will try your other function and see if that works. if it does, i will reopen the question for you.
I made the changes and now it compiles but it does not pause at all. I will try your other function and see if that works. if it does, i will reopen the question for you.
why does it not pause ?
please send the code you have written
it *must* wait
please send the code you have written
it *must* wait
GreatOne does it work ? Nothing heared from you
ASKER
Norbert,
Sorry for no response. Here is the code i used:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void wait(time_t TimeToWait)
{
time_t StartTime=time(NULL);
time_t ActualTime;
do
{
ActualTime=time(NULL);
if((StartTime+TimeToWait)> =ActualTim e)break;
if(ActualTime<StartTime) //Midnight crossing
StartTime=ActualTime;//thi s is not a good solution but it prevents the loop running endless
}while(1);
}
void main()
{
printf("testing wait function..");
wait(10);
printf("wait function called.\n");
}
like i said, there is no pause. any ideas?
Sorry for no response. Here is the code i used:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void wait(time_t TimeToWait)
{
time_t StartTime=time(NULL);
time_t ActualTime;
do
{
ActualTime=time(NULL);
if((StartTime+TimeToWait)>
if(ActualTime<StartTime) //Midnight crossing
StartTime=ActualTime;//thi
}while(1);
}
void main()
{
printf("testing wait function..");
wait(10);
printf("wait function called.\n");
}
like i said, there is no pause. any ideas?
In my Original Post I made a stupid typo:
The Line
if((StartTime+TimeToWait)> =ActualTim e)break;
is wrong it must be
if((StartTime+TimeToWait)< =ActualTim e)break;
I have tested it and it works for me
BTW
I don't know wich time period but answers are auto grade as I know if there is a long time no activity
The Line
if((StartTime+TimeToWait)>
is wrong it must be
if((StartTime+TimeToWait)<
I have tested it and it works for me
BTW
I don't know wich time period but answers are auto grade as I know if there is a long time no activity
Wait function no more needed ??
ASKER
Thanks Norbert. Everything works perfectly now. Sorry about the long delay.
Fine that it works but did you recognize that an other gets the points?
unsigned int sleep(unsigned int seconds);