How to use ofTimer?

So I’ve been trying to implement a timer that will call a keyPressed(m) every 3 seconds with m being an int from a vector. I’m using the code here ofTimer | openFrameworks as an example and writing it in a header file, but it seems that when Scheduler() is called, my program abruptly closes.

class Scheduler: public ofThread {
public:
	ofApp ofapp;

    Scheduler() {
        timer.setPeriodicEvent(3000000000); // this is 3 second in nanoseconds
        startThread();
    }

private:
    ofTimer timer;
    void threadedFunction() {
        while(isThreadRunning()) {
            timer.waitNext();
			int m = ofapp.giveInput(ofapp.keys);
			ofapp.keyPressed(m);
			if(ofapp.keys.size() == 0){
				stopThread();
			}
        }
    }
};

Hey @Second_GAT , I’m pretty sure you can’t have an instance of ofApp inside of your Scheduler class.

If you don’t need precise timing in a separate thread, you could use a counter in ofApp and run some code when the counter reaches a certain value. Another way would be to use ofGetElapsedTimef() and a couple of variables that can track beginning and end values.

If you want to use ofTimer in a separate thread, there should be an instance of Scheduler in your OF application. An instance of ofApp has a pointer that you can use, to access it from the outside. But often a more simple way is to let the application get values from its Scheduler to do things with. Maybe have a look at /examples/threads/threadExample for how to use an ofThread and threadedFunction() in a custom class.