2013-08-05 15 views
-1

是否有可能在C中創建類似於觸發器的東西,例如每5秒鐘調用一次方法?如果計時器在方法執行期間繼續,那甚至會更好。C中的時間觸發器

+0

你可能想看看[這裏](http://stackoverflow.com/questions/1784136/simple-signals-c-programming-and-alarm-功能)。 – Nobilis

回答

1

如何:

long thresh = 5*1000; //mlliseconds 
//Implement getcurTime() 
int prev_time = getcurTime() - thresh; 

while(1){ 

    //Time Elapsed ? 
    if (getcurTime() - prev_time >= thresh){ 
     prev_time = getcurTime(); 
     Myfunction(); 
    } 
    Sleep(thresh); 
} 
+0

標準'time()'函數發生了什麼? – Jocke

+0

@Jocke可能是,但需要一些方法來獲得秒數。 – P0W

+2

@POW你有你需要的所有秒鐘。 'time()'返回自1970年1月1日以來經過的秒數。 – Jocke