QTime or QTimer wait for timeout

Posted by user968243 on Stack Overflow See other posts from Stack Overflow or by user968243
Published on 2012-04-01T04:17:15Z Indexed on 2012/06/05 16:40 UTC
Read the original article Hit count: 152

Filed under:
|

I'm writting a Qt application, and I have a four hour loop (in a seperate thread). In that four hour loop, I have to:

  1. do stuff with the serial port;
  2. wait a fixed period of time;
  3. do some more stuff with the serial port;
  4. wait an arbitrary amount of time.
  5. when 500ms have past, do more stuff;
  6. Go to 1. and repeat for four hours.

Currently, my method of doing this is really bad, and crashes some computers. I have a whole bunch of code, but the following snippet basically shows the problem. The CPU goes to ~100% and eventually can crash the computer.

 void CRelayduinoUserControl::wait(int timeMS)
{
    int curTime = loopTimer->elapsed();
    while(loopTimer->elapsed() < curTime + timeMS);
}

I need to somehow wait for a particular amount of time to pass before continuing on with the program. Is there some function which will just wait for some arbitrary period of time while keeping all the timers going?

© Stack Overflow or respective owner

Related posts about qt

Related posts about qt4