05-07-2014 10:50 PM
I would like to run a while loop exactly every 1 minute, with the minute occuring as per the computer time at 0 seconds.
a timed loop runs a minute after the loop is started and is not syncronised with the 0 seconds of the computer time.
how do I achieve this? I realise the first loop will be less than 1 minute due to the synchonisation.
Solved! Go to Solution.
05-07-2014 11:07 PM - edited 05-07-2014 11:17 PM
Use the current computer time and compare the number of seconds to zero. If it is, do whatever it is you want to do in the While Loop. If it isn't, then execute a false case of a case structure that does nothing.
05-07-2014 11:14 PM
I thought of doing this, but it then the main loop needs to run at 1 second, when I only need it to be 1 minute.
is there another way?
05-07-2014 11:18 PM
Why can't the main loop run at 1 second?
Just let it do nothing the other 59 seconds. You really don't want slow running while loops because if the user hits stop, it will take 1-2 minutes for the loop to realize this and stop the while loop.
05-08-2014 07:50 PM
thanks, that helped me.
i was initially thinking the 1 second loop would consume additional resources, but realised this is negligible.
I ended up using quotient/remainder on the computer time in seconds and checking when the remainder was 0, as this allowed for second interval.
05-09-2014 01:54 AM
Technically there's a miniscule change you can miss a minute by this approach. As the windows timer isn't perfect and due to the multitasking the 1000ms wait can be a couple ms longer in some cases (not all the time). If this happens you could end up with the ms timer reading just before and after the '0'-second.
(It's basically the Nyquist theorem applied on these timings)
The solution is simple, check at a time that cant miss, e.g. 750ms. 🙂
If checking <500ms you'll have to handle the case of 2-3 hits during the same second.
A common solution is to store and compare to last used time, and when >=1min has passed, do action, set new last time.
/Y