Is there any way to do something like sleepThreadForSeconds(.5f)
Or maybe:
while( getTime() < t )
currThread.sleep;
?
I want to play a sound, wait half a second, do something else, wait a second, do something else etc.
In C# there was the yield command.
Is there any way to do this in JUCE without either splitting my function into multiple functions and using timers, or something involving nesting lambdas and timers?
π
PS just found http://www.juce.com/forum/topic/how-wait-using-timer
I'm scratching my head because everyone on the forum seems to be saying 'use timer'.
I suppose I would need to pay attention to which thread I'm on, and probably spawn my own worker.
EDIT:
Got it working...
class Calib : Thread {
public: Calib() : Thread("CalibrationThread") {
startThread();
};
void run() override {
for (int i = 0; i < 8; i++) {
DBG(String(i));
wait(500);
}
stopThread(0); // FAIL
waitForThreadToExit(0);
delete(this);
}
};
void calibrate() {
Calib* calib = new Calib();
}
... but it leaks upon exit. So how to make it take care of removing itself?
Maybe this is impossible?
The cleanest way I can see is to have it send a message to the parent object indicating that it has completed. And the parent object removes it. Or maybe it sends the message to itself...
EDIT: Gottit!
void run() override
{
for (int i = 0; i < 8; i++) {
DBG(String(i));
wait(500);
}
struct Msg : CallbackMessage {
Calib* t;
Msg(Calib* _t) : CallbackMessage() {
t = _t;
}
void messageCallback() override {
t->stopThread(0);
t->waitForThreadToExit(0);
delete(t);
}
};
Msg* m = new Msg(this);
m->post();
}