Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How can I interrupt a function?
QtWS25 Last Chance

How can I interrupt a function?

Scheduled Pinned Locked Moved C++ Gurus
15 Posts 5 Posters 12.9k Views
  • 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.
  • N Offline
    N Offline
    neveralull
    wrote on last edited by
    #5

    Unfortunately, QTimer will not trigger a timeout while the task is doing something, and therefore if a function in the task is hung, the timeout will not trigger ever. You can test this by signalling a function in the task to do something that takes 30 seconds, and setting the timeout to 10 seconds. You won't get the timeout. It doesn't matter if the QTimer is defined inside the thread, or outside the thread. I've tried both.

    I've also tried simulating the case where the function is truly hung, and I had to go to task manager and shut down the processes, including QtCreator one at a time, and restart the computer - and it takes forever.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #6

      Yes, it will trigger, but not if you put it in the same thread.

      1 Reply Last reply
      0
      • N Offline
        N Offline
        neveralull
        wrote on last edited by
        #7

        I wish that were true. That's the very first thing I tried. In the main window a HardwareManagerThread is created. The main window starts a QTimer for 10 seconds, then signals a function in the HardwareManagerThread over a queued connection. The function runs for 30 seconds. After the 30 seconds is up, the 10 second timer triggers (20 seconds late). Had the function hung, the timer would have never triggered. Try it. I might have done something wrong, but the above describes what I have done, and that is the results I get.

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

          @Andre: As long as the hardware function blocks, the event loop of the thread is stalled and the timeout signal event cannot be delivered. Or do I miss something?

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

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #9

            Might be, but the suggestion was to run the hardware function in a separate thread from the main thread, and then use a time in the main tread connected to the terminate() slot of the QThread object that is managing the hardware-related function. There is no need to handle that signal inside the thread itself, the signal stays in the main thread.

            I don't see why that would not work. Obviously, you can't listen for a (cross-thread) signal while you're in a blocking function inside the thread itself. But in the suggested setup, that is not needed.

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

              @Andre: Ah, yes. You're completely right. I misread the post in the sense that the timer should live in the secondary thread.

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

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #11

                [quote author="Andre" date="1334043449"]Might be, but the suggestion was to run the hardware function in a separate thread from the main thread, and then use a time in the main tread connected to the terminate() slot of the QThread object that is managing the hardware-related function. There is no need to handle that signal inside the thread itself, the signal stays in the main thread.

                I don't see why that would not work. Obviously, you can't listen for a (cross-thread) signal while you're in a blocking function inside the thread itself. But in the suggested setup, that is not needed.[/quote]

                This works, unless someone moves the QThread object itself to live inside the thread (what is wrong).

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #12

                  [quote author="Gerolf" date="1334060023"]This works, unless someone moves the QThread object itself to live inside the thread (what is wrong).[/quote]
                  Indeed. That would be Doing It Wrong (TM).

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    neveralull
                    wrote on last edited by
                    #13

                    [quote author="Andre" date="1334043449"]Might be, but the suggestion was to run the hardware function in a separate thread from the main thread, and then use a time in the main tread connected to the terminate() slot of the QThread object that is managing the hardware-related function. There is no need to handle that signal inside the thread itself, the signal stays in the main thread. I don't see why that would not work. Obviously, you can't listen for a (cross-thread) signal while you're in a blocking function inside the thread itself. But in the suggested setup, that is not needed.[/quote]
                    Let me re-iterate. No signal in any thread from any timer declared anywhere is signaled once the hardware function blocks and never returns.

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #14

                      Then I don't know what that function is doing, but it seems to block the whole system.

                      1 Reply Last reply
                      0
                      • N Offline
                        N Offline
                        neveralull
                        wrote on last edited by
                        #15

                        All I'm doing is simulating a function that might hang. The following function blocks all timers in all threads in the system until it is finished. If you replace the for loop with while(true), it will block all timers in all threads forever.
                        @
                        bool HardwareManager::myFunc()
                        {
                        double a;
                        for (int i = 0; i < 100; i++)
                        {
                        for (unsigned j = 0; j < 10000000; j++)
                        {
                        a += j * 3.0;
                        }
                        }

                        qDebug() << "finishing myFunc";
                        return true;
                        

                        }
                        @

                        Edit: please use code tags around code sections; Andre

                        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