about the longest elapsed time in Qt
-
@jsulm
ok, that's INT_MAX, int32.
ok, so max(int64) is about 292471208 years...that's enough@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
-
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? -
Why don't you use QTimer? Seems like the optimal tool for your case (run code every xxx seconds).
Elapsed timer is for measurement.
-
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 -
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 -
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(); } }
-
@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? -
@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).
-
@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(); } }