What could be the possible cause of Qtimer failure?
-
I encountered an issue with QTimer where it failed to work properly in one part of my program, even though it functioned correctly elsewhere.
QEventLoop loop2; QTimer::singleShot(500, [&]() {loop2.quit(); }); loop2.exec();I was using an event loop to pause the program for a period, but when the timer expired, it didn’t quit as expected, causing the program to hang at that point. I eventually resolved the issue by replacing it with QThread::msleep(500);.
-
I encountered an issue with QTimer where it failed to work properly in one part of my program, even though it functioned correctly elsewhere.
QEventLoop loop2; QTimer::singleShot(500, [&]() {loop2.quit(); }); loop2.exec();I was using an event loop to pause the program for a period, but when the timer expired, it didn’t quit as expected, causing the program to hang at that point. I eventually resolved the issue by replacing it with QThread::msleep(500);.
where it failed to work properly in one part of my program, even though it functioned correctly elsewhere.
What is the difference between these places? Would I be correct in guessing that the working instances are in the main thread, and those that do not behave as expected are in other threads?
Just out of interest, is there a reason you did not use the more direct:
QTimer::singleShot(500, &loop2, &QEventLoop::quit);BTW, this kind of blocking behaviour is usually an anti-pattern in Qt programs.
-
I encountered an issue with QTimer where it failed to work properly in one part of my program, even though it functioned correctly elsewhere.
QEventLoop loop2; QTimer::singleShot(500, [&]() {loop2.quit(); }); loop2.exec();I was using an event loop to pause the program for a period, but when the timer expired, it didn’t quit as expected, causing the program to hang at that point. I eventually resolved the issue by replacing it with QThread::msleep(500);.
@John-Van said in What could be the possible cause of Qtimer failure?:
QThread::msleep
this is prime example what will cause timers to "fail"
-
I encountered an issue with QTimer where it failed to work properly in one part of my program, even though it functioned correctly elsewhere.
QEventLoop loop2; QTimer::singleShot(500, [&]() {loop2.quit(); }); loop2.exec();I was using an event loop to pause the program for a period, but when the timer expired, it didn’t quit as expected, causing the program to hang at that point. I eventually resolved the issue by replacing it with QThread::msleep(500);.
-
where it failed to work properly in one part of my program, even though it functioned correctly elsewhere.
What is the difference between these places? Would I be correct in guessing that the working instances are in the main thread, and those that do not behave as expected are in other threads?
Just out of interest, is there a reason you did not use the more direct:
QTimer::singleShot(500, &loop2, &QEventLoop::quit);BTW, this kind of blocking behaviour is usually an anti-pattern in Qt programs.
-
@ChrisW67
“this kind of blocking behaviour is usually an anti-pattern in Qt programs.”
would you provide a detailed explanation?@John-Van said in What could be the possible cause of Qtimer failure?:
would you provide a detailed explanation?
Never block using long lasting loops or local event loops. This is usually not needed in event driven applications. Long lasting calculations should be done in other threads.