Setting up a timer to end the program at a set duration.

I need to implement a timer system to close the program or atleast stop it from outputting data and tracking blobs.

I’ve been searching for a timer system for openframeworks but to no avail. I’ve tried using ofElapsedTimef() in this manner.

if (ofElapsedTimef() == duration)  
void testApp::exit(){  
        trackEnabled = false;  
        drawEnabled = false;  
        drawEnabledVal = 0;  

But the problem is it does not work at all, when i output the values via cout, the numbers are not exact.

if i set the duration to 10.000, the getElapsedTimef increases randomly so the closest i get is about 10.004 or 10.003. even if i set it so that if it passes by a range of numbers, it should run exit();

But it does not work still.

Any suggestions?

give it a time range. don’t use ==

if(ofElapsedTimeMillif() > 6 && ofElapsedTimeMillif < 8 ) exit();

or actually just do:

if(ofElapsedTimeMillif() > 6) exit();

that should do it or am i misunderstanding you?


I already attempted using ranges rather than exact values.

It compiles but the problem now is that it does not still register the values(?). or maybe there is some fault to the way I form my exit(); function.

Nothing really happens once for example, the timeElapsed goes between 9-10 seconds, which is the time for the whole duration of the program. It just continues on as if nothing happened.

How exactly do i automatically close an openframeworks app?

when you want your time counter to start you say once:

myTimeKepper = ofGetElapsedTimef();

in update you check the time that passed:

if( ofGetElapsedTimef() - myTimeKepper > 60 ) std::exit(1);

if you want the timer to start again repeat the steps.


Again, stephan. Thanks a lot. It works now. Props to you!