How to use timers in my Console App

Here’s what I’ve got so far:

Basically I want an event to be fired every x ms. In my case i’ll probably be calling to read GPIO about every 10ms.

The GIST runs as expected, but the callback never gets fired.

I don’t think a Console app on Windows can hook into the main event loop. You could consider:

  • Using gui app template, but not opening any windows, or
  • Rolling your own timer loop by sleeping a thread (sleep_until might be useful).

I’d recommend always using juce::JUCEApplication, even for console apps, as it does so much useful stuff for you!

Hey guys, thanks for the responses.

My app is designed to work on MacOS and Linux, but I imagine that doesn’t matter. I tried rolling my own timer using juce::Time::waitForMillisecondCounter in a loop, but it was not very reliable. Timing issues came up depending on background tasks (switching windows on mac for example).

I can look into the gui app template, but I’m not sure what the overhead will look like.

I’d recommend always using juce::JUCEApplication , even for console apps, as it does so much useful stuff for you!

Hmm I’m using juce::ConsoleApplication in my app, though I didn’t put it in the example. Does that have some of the same features? Or will using juce::JUCEApplication give me some additional capability (like timers).

Interestingly, the timers don’t work at all unless I have juce::ScopedJuceInitialiser_GUI initialiser; - This might be unrelated, and its possible I’m doing it wrong.

Really hoping I can use some sort of reliable timer. I’ll keep playing with it and watching for other ideas here.

Thanks yall! Great community here!

No, that is exactly how you do it, if you want to use the juce message loop.

Despite the name, juce::ConsoleApplication is not related to juce::JUCEApplication or juce::JUCEApplicationBase. The later spin a message loop, but ConsoleApplication merely does commandline arguments parsing (quite useful, but badly named IMHO).