2015-11-23 81 views
3

我試圖確定自上次檢查以來是否在控制檯應用程序中經過了5秒鐘。我認爲我的邏輯稍微偏離了,我不知道如何解決它。確定是否已經過去5秒

我的lastCheck變量在程序開始時首先爲0。它負責維持「舊時代」。

LastCheckCheckSeconds()更新,這給它一個新的「舊時代」

如果LastCheck等於1232323,而now變量當前等於1227323,那麼我會知道5000毫秒過去了。 (實際上,這個數字比這大得多)

否則,我不想要發生任何事情,我想等到這五秒鐘真的過去了。

BACKEND

inline std::vector<int> CheckSeconds(int previous, int timeinseconds) 
{ 
    //check if a certain amount of seconds have passed. 
    int now = GetTickCount(); 
    int timepassed = 0; 
    std::vector<int> trueandnewtime; 
    //if the current time minus the old time is greater than 5000, then that means more than 5000 milliseoncds passed. 
    //therefore the timepassed is true. 
    if (now - previous > 5000) 
     timepassed = 1; 

    trueandnewtime.push_back(timepassed); 
    trueandnewtime.push_back(now); 

    return trueandnewtime; 

} 

FRONTEND

storage = CheckSeconds(LastCheck, 5); 
     LastCheck = storage.at(1); 

     if (storage.at(0) == 1) 
     { 
      ....blahblahblah..... 
     } 

任何人都知道我做錯了嗎?我必須在某個地方出現邏輯錯誤,否則我會變笨。

另外值得注意的是,這段代碼是在一個while循環中,不斷運行在Sleep(60);這是一個控制檯應用程序在momemnt。

感謝您的幫助。

+0

「int」大到足以存儲'GetTickCount'的結果嗎? –

+2

使用[#include ](http://en.cppreference.com/w/cpp/chrono)中的函數檢查時間,並使用操作系統函數進行睡眠 –

+0

int足夠大,否則會顯示奇怪的值,同時調試。 –

回答

0

通過將Lastcheck集放入循環來修復它。

相關問題