Solved

Wait/Pause function in C?

Posted on 1998-06-16
15
283 Views
Last Modified: 2010-04-15
Is there a function in C that will pause for a specified amount of time?
If so, what library must I #include?

Thanks
0
Comment
Question by:GreatOne
15 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1251240
#include <unistd.h>

unsigned int sleep(unsigned int seconds);

0
 
LVL 4

Accepted Solution

by:
sganta earned 20 total points
ID: 1251241
Hi !

You use

sleep(20);  /* It sleeps for 20 seconds */;


0
 
LVL 3

Expert Comment

by:Norbert
ID: 1251242
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;//this is not a good solution but it prevents the loop running endless
      }while(TRUE)
}
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 3

Expert Comment

by:Norbert
ID: 1251243
I forgot
#include <time.h>
0
 

Author Comment

by:GreatOne
ID: 1251244
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?


0
 
LVL 3

Expert Comment

by:Norbert
ID: 1251245
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*)0x0000046c; //adress of bios timer  using dos
unsigned long Starttime=*Biostime;
unsigned long  time_t ActualTime;
      do
      {   
            ActualTime=*Biostime;
            if((StatTime+TimeToWait)>=ActualTime)
                    break;
             if(ActualTime<StartTime) //Midnight crossing
                 StartTime=ActualTime;//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
0
 
LVL 3

Expert Comment

by:Norbert
ID: 1251246
for security reasons to not depend on model small, medium compact, large or huge change the line
volatile long *Biostime=(long*)0x0000046c; //adress of bios timer  using dos
to
volatile long far*Biostime=(long far*)0x0000046c; //adress of bios timer  using dos
0
 

Author Comment

by:GreatOne
ID: 1251247
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.
0
 
LVL 3

Expert Comment

by:Norbert
ID: 1251248
why does it not pause ?
please send the code you have written
it *must* wait
0
 
LVL 3

Expert Comment

by:Norbert
ID: 1251249
GreatOne does it work ? Nothing heared from you
0
 

Author Comment

by:GreatOne
ID: 1251250
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)>=ActualTime)break;
if(ActualTime<StartTime) //Midnight crossing
StartTime=ActualTime;//this 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?
0
 
LVL 3

Expert Comment

by:Norbert
ID: 1251251
In my Original Post I made a stupid typo:
The Line
          if((StartTime+TimeToWait)>=ActualTime)break;
is wrong it must be
          if((StartTime+TimeToWait)<=ActualTime)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
0
 
LVL 3

Expert Comment

by:Norbert
ID: 1251252
Wait function no more needed ??

0
 

Author Comment

by:GreatOne
ID: 1251253
Thanks Norbert. Everything works perfectly now. Sorry about the long delay.
0
 
LVL 3

Expert Comment

by:Norbert
ID: 1251254
Fine that it works but did you recognize that an other gets the points?

0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Microsoft vcsetup.exe and vcssetup.exe 8 421
mixing C++ and C code elegantly 10 156
Which version of C should I use when Perl is too slow? 13 181
C Language combined operators 28 109
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now