Solved Infinite condition checking
-
I have a task where a function is called and the first thing done in that is to call a
bool
function to check a condition before proceeding. If that condition is true the code proceeds and if not it returns.But I need a way to periodically check that condition, if it ever returns that function will be called again.
bool condition() { ... //return true or false; } void foo() { if( ! condition) return; ... }
I need to find a way to go back to
foo
in casecondition
ever returns true.I thought about
QEventLoop
but I want more options.Any help is appreciated. Thanks in advance!
-
@hbatalha said in Infinite condition checking:
I thought about QEventLoop but I want more options.
elaborate
-
@raven-worx I thought about creating a loop that would run indefinitely until the condition returns true, if it ever does, meanwhile the user can use the application "normally".
But I don't want to useQEventLoop
, because it adds an extra complexity to the program. That's why I want more options. -
@hbatalha
but the extra logic is needed, since you want to block the execution but actually you do not want to.
You can use WorkerScripts in QML which act like threads. -
-
Hi,
Since foo checks the condition on each call, in fact, you want something that calls regularly foo ?
-
@SGaist something that calls condition regularly and if condition returns true then foo will be called
-
Sounds like a simple QTimer that calls foo regularly should do the job.
-
@SGaist That did solve my problem, thanks
void SomeClass::foo() { if( ! condition) { QTimer::singleShot(5000, this, &SomeClass::foo); return; } ... }
-
You might want to consider making a QTimer member variable to be able to stop it for example when your application ends.
-
@SGaist didn't quite understand what you meant, when my application won't everything be stopped?
Also, I need to call foo only once when the timeout is emitted, took that example from the documentation.
-
I don't know how long foo will take so you might not want for it to triggered when you end your application. Ending an application does not mean everything is destroyed instantly.
-
@SGaist foo is not from the mainwindow, is part of another class, should I call stop in the destructor?
-
@hbatalha said in Infinite condition checking:
is part of another class
Then put it there as member variable and stop it in destructor (or whenever needed).
-
@jsulm ok thanks
-
Ending an application does not mean everything is destroyed instantly.
I have followed your suggestion, and now I am wondering, is this something that I have to always watch out for, is there a name for this kind of situation, a reference I can search?
And also, why is everything not destroyed instantly? What are the steps being taken? What is happening under the hood there? Any reference?
-
Order of destruction, code run during destruction, etc
Think of a database, you likely appreciate the fact that all operations are finalised before it stops so you don't have corrupted data when you restart it.