How can I interrupt a function?
-
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.
-
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.
-
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 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).
-
[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. -
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