Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Timer.h"
- Timer *Timer_Create(long interval, ITimerTask(run)){
- Timer *timer = (Timer*)malloc(sizeof(Timer));
- timer->enable=false;
- timer->interval=interval;
- timer->run=run;
- timer->id = NULL;
- return timer;
- }
- unsigned long getTime(){
- Time timestamp;
- gettimeofday(×tamp,NULL);
- unsigned long timeMls = timestamp.tv_usec/1000;
- return timeMls; // переводим из микросекунд в миллисекунды
- }
- void Timer_Start(Timer *timer){
- if(timer->enable){
- return;
- }
- timer->enable = true;
- timer->id=g_timeout_add(timer->interval,timer->run,NULL);
- }
- void Timer_Stop(Timer *timer){
- if(!timer->enable){
- return;
- }
- timer->enable = false; // помечаем флаг активности как "неактивен"
- g_source_remove(timer->id);
- timer->id=NULL;
- }
- void Timer_Destroy(Timer *timer){
- if(timer->enable){
- Timer_Stop(timer); // если таймер был запущен, нужно его остановить
- }
- free(timer); // высвобождаем память, выделенную под таймер
- }
- ITimerService TimerService = {
- Timer_Create,
- Timer_Start,
- Timer_Stop,
- Timer_Destroy
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement