Click here to Skip to main content
15,921,382 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

I want to implement a timer in which program calls a function after every 5 seconds .

I tried to implement in this code but it is strucking in the inner for loop

C#
#include<stdio.h>
#include<time.h>

int main()
{
 int a =10;
 int i=0;

 time_t seconds;
 seconds = time (NULL);
 for(;;)
 {
   for(;;)
   {
     if(time(NULL) - seconds <3)
     {
       printf("in if \n");
       foo();
       a++;
     }
     else
     {
       break;
     }
   }
   printf("%d",a);
  }
 return 0;
}
Posted
Updated 16-Sep-12 19:08pm
v2
Comments
Joan M 17-Sep-12 2:08am    
Probably to make your code work you should at least update the "seconds" variable inside the loop... you are checking always the current time with an initial amount of seconds, and you expect it to be smaller than 3... After updating it you could check if the value is bigger than 3.

1 solution

 
Share this answer
 
Comments
AmitGajjar 17-Sep-12 1:17am    
5+ nice CP Article.
Mehdi Gholam 17-Sep-12 1:34am    
Cheers!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900