will be simple return from function without throw error SetTimeout(10100) - timeout on 10 seconds and 100 milliseconds May be this examples help to you #include If you're trying to sleep and do work at the same time you need threads. It's better to sleep() most of the time then start checking the time. Repeatedly polling by reading the time and comparing to the done time (are we there yet?) will burn a lot of CPU cycles which may slow down other programs running on the same machine (and use more electricity/battery). You see it as pausing your program, but what they really do is release the CPU for that amount of time. Sleep(), usleep(), nanosleep() have a hidden benefit. See man 2 time, man gettimeofday, man clock_gettime. Making a timer works the same way except when you add your time to wait you need to remember to manually do the carry (into the time_t) if the resulting microseconds or nanoseconds value goes over 1 second. For times less than 1 second you need to use gettimeofday() (microseconds) or clock_gettime() (nanoseconds) and deal with a struct timeval or struct timespec which is a time_t and the microseconds or nanoseconds since that 1 second mark. That will probably include resetting it by doing another fire_t = time(NULL) + seconds_to_wait for next time.Ī time_t is a somewhat antiquated unix method of storing time as the number of seconds since midnight but it has many advantages. If (my_t > fire_t) then consider the timer fired and do the stuff you want there. Inside your loop do another my_t = time(NULL) A time_t is essentially a uint32_t, you may need to cast it. Then (for times over 1 second), initialize your timer by reading the current time: my_t = time(NULL) Īdd the number of seconds your timer should wait and store it in fire_t. If you already have a main loop (most GUI event-driven stuff does) you can probably stick your timer into that.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |