Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sleep problem without QThread class
Qt 6.11 is out! See what's new in the release blog

Sleep problem without QThread class

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 25.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ahmed kato
    wrote on last edited by
    #1

    how to make my program sleep for 100 ms without using Qthread class??

    Ahmed Kato
    Computer & communications student
    Intern and MSP at Microsoft

    1 Reply Last reply
    0
    • B Offline
      B Offline
      broadpeak
      wrote on last edited by
      #2

      maybe:
      -#include <qwaitcondition.h>-

      -QWaitCondition sleep;-
      -sleep.wait(1000); // one seconds-

      ahh, sorry this was ok for Qt 3, but for Qt 4 the wait() function is changed... (you need a mutex too)

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lgeyer
        wrote on last edited by
        #3

        You either use QTest
        @
        QTest::qSleep(100); // no event processing
        QTest::qWait(100); // event processing
        @
        or you use QWaitCondition / QMutex
        @
        QWaitCondition waitCondition;
        QMutex mutex;

        waitCondition.wait(&mutex, 100);
        @
        or you use QThread.
        @
        class Thread : public QThread
        {
        public:
        static void msleep(int ms)
        {
        QThread::msleep(ms);
        }
        };

        Thread::msleep(100);
        @

        However, having to do so in an event-driven application is usually a clear indicator for a broken design.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          joonhwan
          wrote on last edited by
          #4

          Is there any specific reason not to use QThread::sleep() ?

          joonhwan at gmail dot com

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lgeyer
            wrote on last edited by
            #5

            Yes, it is protected.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              broadpeak
              wrote on last edited by
              #6

              [quote author="Lukas Geyer" date="1325612918"]
              or you use QThread
              [/quote]

              Hm... What about the (pure virtual) run() function?

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lgeyer
                wrote on last edited by
                #7

                [quote author="broadpeak" date="1325618245"]
                Hm... What about the (pure virtual) run() function?[/quote]

                It is indeed virtual, but not pure. The default implementation simply calls exec() (which doesn't even matter in our case, as the thread isn't started). QThread::msleep() causes the current thread to sleep.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

                  On unix like machins (including the Mac):

                  @
                  #include <unistd.h>

                  // sleep for 5 seconds
                  sleep(5);

                  // sleep for another 5 seconds (5,000,000 microseconds)
                  usleep(5000000);
                  @

                  On Windows:
                  @
                  // sleep for 5 seconds (5000 milliseconds)
                  Sleep(5000);
                  @

                  Or copy the implementation of QTest::qSleep():

                  @
                  #ifdef Q_OS_WIN
                  #include <windows.h> // for Sleep
                  #else
                  #include <time.h>
                  #endif

                  void mySleep(int ms)
                  {
                  if(ms <= 0)
                  return

                  #ifdef Q_OS_WIN
                  Sleep(uint(ms));
                  #else
                  struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 };
                  nanosleep(&ts, NULL);
                  #endif
                  }
                  @

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved