[comp.sys.amiga.tech] Frienndly Sleep

ali@polya.STANFORD.EDU (Ali T. Ozer) (04/10/88)

In article <662@wolf.UUCP> uzun@wolf.UUCP (Roger Uzun) writes:
> I was wondering if there was a friendly way for an amiga Process to
>Sleep for a period of time (in C).

You can use the Delay() function. Call Delay() with the number of 1/50 seconds:
Delay(50L) will put your process to sleep for one second. (By the way, don't
ever call Delay(0L) --- due to a bug in AmigaDOS, this might trash a disk.
Instead call: void MyDelay(d) unsigned long d; {if (d) Delay(d);}.

Another way is to use the timer device. Advantages:
 - You can have finer control (units smaller than 1/50 second)
 - You don't need to be running as an AmigaDOS process (Delay() is an AmigaDOS
   call and will not work from a task --- just from a process)
 - You can simultaneously wait for other things to happen: You can send a
   timer request, than do a Wait() on multiple signals; for instance, the
   timer port and your IDCMP port. Thus your program would wake up on either
   when the specified time has passed or when the user did something that
   requires attention, whichever comes first. 

Using the timer device isn't all that complicated; just a few more lines of
code than a Delay(). Check out LedClock from Fish 128 as a short example
of simple use of the timer device. 

Ali Ozer, ali@polya.stanford.edu

thomson@utah-cs.UUCP (Richard A Thomson) (04/11/88)

In article <662@wolf.UUCP> uzun@wolf.UUCP (Roger Uzun) writes:
> I was wondering if there was a friendly way for an amiga Process to
>Sleep for a period of time (in C).  Currently to sleep for say, 
>1 second I do a 
>
>for(i=0;i<50;i++)
> WaitTOF();
>
>Is there a better, possibly more precise way of waiting for a period
>of time without hogging the CPU.
>
>Thanks
>-Roger

There is a DOS routine called Delay() that does exactly what you want.
Use the form:

	Delay(ticks);	/* there are 50 ticks per second */

So to get a delay of 1 second, you would use:

	Delay(50);

				-- Rich Thomson