about the longest elapsed time in Qt
-
QElapsedTimer m_ElapsedTimer;
if ( !m_ElapsedTimer.isValid() || m_ElapsedTimer.hasExpired(timeout)) { emit signalSendEmail(); m_ElapsedTimer.restart(); }
i want to write like this, this will not restrict the 1st time emit, and since the 1st emit, the next emit is only allowed outof timeout milliseconds.
However, i want the exe runs very very long time. the qinit64 seems still not that long enough for the longest interval in theory(qint64 is only about 214783 seconds). What will happen if the real time interval is longer than this?
And what is the best method to get the longest time interval supportted in code? -
@opengpu said in about the longest elapsed time in Qt:
qint64 is only about 214783 seconds
What?
max(qint64) == 9223372036854775807
Divede that number by 1000 and you get the number of seconds.
So, why do you think it is 214783 seconds? -
@opengpu said in about the longest elapsed time in Qt:
ok, that's INT_MAX, int32
This is wrong as well: max(int) = 2,147,483,647. Devide it by 1000 and you get 2,147,483sec
"is the code above right? is it the best and efficient way?" - looks fine
-
void SendEmail() { if ( !m_ElapsedTimer.isValid() || m_ElapsedTimer.hasExpired(timeout)) { emit signalSendEmail(); m_ElapsedTimer.restart(); } }
SendEmail() is called at some situation, it's not called every time-interval. i just have to control inside the time-interval from last SendEmail another SendEmail is forbidden.
ie. SendEmail's minimun time interval, the time between two SendEmails >= this interval -
@opengpu said in about the longest elapsed time in Qt:
m_ElapsedTimer.restart();
Attention:
QElapsedTimer::restart()
only works ifQElapsedTimer
instance is valid ==>QElapsedTimer::start()
has been called before!
If you don't use the return value (which is the elapsed timer since last start or restart), it is better to usestart()
:void MyClass::SendEmail() { if ( !m_ElapsedTimer.isValid() || m_ElapsedTimer.hasExpired(timeout)) { emit signalSendEmail(); m_ElapsedTimer.start(); } }
-
@KroMignon thanks so much!, so start when expired is ok?
because i donot when the 1st time call SendMail, so i use isValid to let me know it's the 1st time SendEmail is called.
after that it's actually check by the hasExpired.
right? -
@opengpu said in about the longest elapsed time in Qt:
so start when expired is ok?
Yes, of course.
QElapsedTimer::hasEpired()
is only a kind of helper function,
==>m_ElapsedTimer.hasExpired(timeout)
is almost the same asm_ElapsedTimer.elapsed() > timeout
. This does not affectQElapsedTimer
state.The difference is only that hasExpired() will test if timer has been started (is valid).
-
@KroMignon thank you, and i will write as follow:
QElapsedTimer m_ElapsedTimer; ... void MyClass::SendEmail() { if ( !m_ElapsedTimer.isValid() || m_ElapsedTimer.hasExpired(timeout)) { emit signalSendEmail(); m_ElapsedTimer.start(); } }