Thread: Delays

  1. #1
    Unregistered
    Guest

    Unhappy Delays

    I've been looking all over the net for some way to make C delay the print of either each character, or entire printf statements. I need to make a small program that will display text as if it was being typed by someone else in real time, so therefore delaying the display of each character. Does anyone know how this can be done in C? (I don't care for portability from Win32 to linux on this one)

    Thx in advance!

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    Have you tried using GetTicCount()? Game developers use this to sync their frame rates... when programming in windows..

    I tested this code inside main and it worked....
    every half second a char was sent to the screen....

    place the code inside main or another function of course....

    Code:
    #include<windows.h>  
    
    
    
    	DWORD time = GetTickCount();
    
    	char mybuff[100] = "zman";
    	int i = 0;
    
    	while( i < 5 )
    	{
    		if( GetTickCount() > time + 500 )//in milliseconds
    		{
    			time = GetTickCount();
    			putchar( mybuff[i] );
    			i++;
    		}
    	}
    zMan

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    And there is also Sleep().

    #include <windows.h>
    .
    .
    Sleep(1000); //Sleep 1 second

  4. #4
    0x01
    Join Date
    Sep 2001
    Posts
    88
    Code:
    /* Delays 10 seconds, then exits........................*/
    #include "stdio.h"
    
    #define 10_SECONDS 500000000 // well, its about 10 seconds
    
    void delay(int nbr)
    {
    	int i = 0;
    	for(i = 0; i < nbr; i++)
    		;
    }
    
    int main()
    {
    	printf("\n Hold on..."); delay(10_SECONDS);
    	printf("\n Ok, returned from 'delay()'");
    	
    	return 0;
    }
    *Note : The delay function above is good for personal use only, because if you were to run this program on a faster computer..... the delay function would return faster. If you ran this program on a slow computer, delay() might take a while to return, thus if you pass the constant '10_SECONDS' that is defined as 500000000 to the delay function, it could be perfect for you, but could be extremely slow or fast depending on another users PC because of its cpu speed etc....

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    Hi!
    You can make knaveīs delay-function universal if it measures at first the systemīs speed and makes the value 10_SECONDS depend on it. But I donīt know how to read the miliseconds that have passed after midnight, but I know that itīs possible.
    Does anyone know?
    I could write the universal function then.

    klausi
    When I close my eyes nobody can see me...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating waits and processing delays
    By ulillillia in forum Windows Programming
    Replies: 16
    Last Post: 12-24-2006, 04:52 PM
  2. time Delays and Random functions
    By markphaser in forum C++ Programming
    Replies: 17
    Last Post: 02-20-2006, 07:10 PM
  3. delays
    By luigi40 in forum C# Programming
    Replies: 6
    Last Post: 04-11-2005, 01:49 AM
  4. Quick Delays
    By bigB8210 in forum C Programming
    Replies: 1
    Last Post: 07-10-2003, 02:06 PM
  5. precision delays
    By marcdawson in forum C Programming
    Replies: 5
    Last Post: 11-08-2002, 11:03 AM