Invoke a function after main
-
@jenya7 said in Invoke a function after main:
It goes to auto result = a.exec(); - then it goes out of main. I never get to someFunction();
How did you verify that?
As soon as exec() terminates the flow continues with next statement.
Maybe you app is crashing? -
@jsulm said in Invoke a function after main:
@jenya7 said in Invoke a function after main:
It goes to auto result = a.exec(); - then it goes out of main. I never get to someFunction();
How did you verify that?
As soon as exec() terminates the flow continues with next statement.
Maybe you app is crashing?I set a break point on auto result = a.exec(); . The next step - break point vanished and I don't have the option to go on with break point. But I see GUI - it prints all the previous messages (till someFunction()) and all operates well. May be someFunction() was executed but it didn't print anything.
-
@J-Hilk said in Invoke a function after main:
question:
do you actually know what
a.exec()
is and does?and if you think you do, can you please post it here quickly ?
I want to make sure, we're on the same page here
That's what I'm tying to understand.
Any way I could connect to some event like a.running() or something. -
@jenya7 What about reading https://doc.qt.io/qt-5/qapplication.html#exec ?
"Any way I could connect to some event like a.running() or something" - can you please explain what you actually want to do?
If you simply want to execute something after exec() then what I posted is the way to go... -
@jsulm said in Invoke a function after main:
@jenya7 What about reading https://doc.qt.io/qt-5/qapplication.html#exec ?
"Any way I could connect to some event like a.running() or something" - can you please explain what you actually want to do?
If you simply want to execute something after exec() then what I posted is the way to go...Yes but someFunction() not printing important messages.
auto result = a.exec(); someFunction();
This way it does print but blocks GUI
someFunction(); auto result = a.exec();
-
@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....