I have a function that is being used to update some information displayed on a GUI. The information should only be displayed for a short length of time (about 10 seconds).
The function being used to set what is displayed on the GUI is called repeatedly, and what is displayed is dependent on conditions within that function.
The issue that I'm having at the moment, is that there is some information being displayed that needs to change depending on how long it has been displayed for.
I tried creating a variable that was set to the system time at the beginning of the function, and then later in the function, had a condition that would change the value of the variable if the current system time was greater than the original system time + 10 seconds.
However, the problem with that was that since the function is being called repeatedly, the original system time is updated with every call/ iteration.
I found the following quesion on SO: How to run code inside a loop only once without external flag? and tried to implement what was suggested in the answer by Angew, so that the variable holding the original system time would only be set once within the loop. My code currently looks like this:
struct rnOnce{
template <typename T>
runOnce(T &&f){ f(); }
};
...
void updateHeader(){
...
static runOnce a([](){
timeWarningStarted = SimulationTime::Instance()->getSimulationTime();
});
...
But when I run my program, and attach to process in debug, the call to static runOnce a([](){... });
appears to be skipped- none of the breakpoints I've put on these lines are ever hit, even though the breakpoints on the lines just before and just after are.
Can anyone explain to me why this is? What am I doing wrong here?
EDIT
The desired behviour is that the timeWarningStarted
variable is only given a value once, despite the fact that it is being set inside a function that is called several times. It is a global variable- and the function that is setting its value will be continuously called as that is the one that's used to update what is displayed on the GUI.
Effectively, I want timeWarnigStarted
to be set to the current system time the first time that this code is executed, and then to retain its value for the duration of time that the program is running.
Before changing my code to what it is above, I had something similar to:
void updateHeader(){
...
timeWaringtarted = SimulationTime::Instance()->getSimulationTime();
...
}
But this was in issue because timeWarningStarted
was being given the current system time every time this function was run (i.e. it was continuously being updated). So, later in the same function, when I wanted to check whether the warning had been displayed for 10 seconds, and if so, to stop it from being displayed, I was doing something like:
void updateHeader(){
...
timeWarningStarted = SimulationTime::Instance()->getSimulationTime();
...
currentSimulationTime = SimulationTime::Instance()->getSimulationTime();
if(currentSimulationTime <= (timeWarningStarted + 10)){
warningMessage = "";
}
...
}
But obviously, since both timeWarningStarted
& currentSimulationTime
will be given a new value every time the above code is run, they will have too close a value because they will be set at almost exactly the same time.
So, by setting timeWarningStarted
inside runOnce
, I was hoping that timeWarningStarted
would only be given a value the first time that this code was run, and would retain that value for the duration of the simulation, but it appears that this section of code is now never run...
static runOnce a([](){
timeReplyFailWarningStarted = SESL::SimulationTime::Instance()->getSimulationTime(); // = 0;
});