Invoke a function after main
-
@jenya7 said in Invoke a function after main:
This way it does print but blocks GUI
someFunction();So why does someFunction() block? What are you doing inside this function? What's your final goal? What should this function do?
-
@jenya7 Did you bother to read the link I gave you?
Probably not, so I will spend my time to explain: exec() is a BLOCKING call (it runs the Qt event loop). It blocks until you terminate the Qt event loop (this happens when you close the last window of your app). Then everything else after exec() is executed. So, expecting someFunction() to be executed after exec() WHILE your app is running is completely wrong! Also the break point after exec() will be hit when you close your app! -
@Christian-Ehrlicher said in Invoke a function after main:
@jenya7 said in Invoke a function after main:
This way it does print but blocks GUI
someFunction();So why does your function block at all? What's your final goal? What should this function do?
someFunction(); - runs concurrent, in a separate thread, at least I hope it does. It goes through a list of remote devices and tries to connect (to discover).
-
@jenya7 said in Invoke a function after main:
someFunction(); - runs concurrent, in a separate thread, at least I hope it does. I
When it blocks then it does not.
And when you would have read and used my first answer, all your problems would have been gone... but I give up here.
-
@Christian-Ehrlicher said in Invoke a function after main:
@jenya7 said in Invoke a function after main:
someFunction(); - runs concurrent, in a separate thread, at least I hope it does. I
When it blocks then it does not.
And when you would have read and used my first answer, all your problems would have been gone... but I give up here.
OK. But I have two questions
QTimer::singleShot(600000, &app, SLOT(quit()));
First - What time do I set - 600000? How long it takes to a.exec()?
Second - How do I put thisdiscovered = m_sensor.DiscoverConcurrent(0, 0, SYS_DISC_MODE_FILE);
Into this
QTimer::singleShot(600000, &?, ???);
-
@jenya7 said in Invoke a function after main:
What time do I set
You can set 0, then the connected slot will be executed as soon as the event loop starts. Means: as soon as exec() starts.
"How do I put this" - you do that in the slot connected to the timer... -
@jsulm said in Invoke a function after main:
@jenya7 said in Invoke a function after main:
What time do I set
You can set 0, then the connected slot will be executed as soon as the event loop starts. Means: as soon as exec() starts.
"How do I put this" - you do that in the slot connected to the timer...I see. QTimer::singleShot - it' s a widget and all widgets enabled by a.exec().
-
@jenya7 said in Invoke a function after main:
QTimer::singleShot - it' s a widget
QTimer::singleShot - sinse when it is a widget?! singleShot is a static method in the QTimer class, which is not a widget.
-
@jenya7 said in Invoke a function after main:
How long it takes to a.exec()?
and here's your understanding problem.
exec() returns, when only one thing happens
- You tell your program to exit/stop running, one way or an other
In the case of the operating system terminating your program prematurely - one way or another- you technically do not return from exec()
inside that exec() call is a Qt styled "infinite" loop, an event loop.
That allows signal slot and other event processing to occur. And because of that event processing, your app can react to keyboard mouse interactions etc and doesn't seem to "hang"
If you now have somewhere an infinite loop yourself, that is called in one of those eventloopcycles, than the program "hangs" because eventloop can not proceed.
So if your function blocks, it's not running in a separate thread, or the main thread has to wait for a return value, so it hangs anyways.
-
@jenya7 said in Invoke a function after main:
QTimer::singleShot - it' s a widget and all widgets enabled by a.exec().
It would not kill you to check in the documentation before you start to talk rubbish you know.
-
OK. Sorry. Just a new stuff. Takes time to understand.
I did this wayint main(int argc, char *argv[]) { uint32_t discovered = 0; QApplication a(argc, argv); MainWindow w; w.show(); QtConcurrent::run(&SetupRun); return a.exec(); }
In SetupRun I put all my setup functions and now it works OK. GUI appears and I see all prints in it.
I really love this Concurrent thing. :) -
@jenya7
That's more like it, and makes sense now :) So you never did want to execute something aftera.exec()
!Don't forget to be careful about what you put in the thread(s) (
SetupRun()
etc.). You are not allowed to access anything in the main thread, especially the whole UI, directly. Subtle bugs lie if you do....