Named timer

Hi Jules,

Is it possible to set up a name property for the Timer class (and change the Timer::callback() to Timer::callback(const String & name)) so it would be possible to set up different timers at the same time (BTW, start and stop should be changed too?

Currently, it’s very difficult to identify the timer, so I have to use math and error to do so.

Under Win32, it is the wParam parameter that holds the timer identifier, and under Linux it is XtAppAddTimeOut last parameter (don’t know for OSX)

Don’t like the sound of that - there’s a lot of code out there that would be affected, and a more complicated timer class would be wasteful in most cases.

But it could be done as a separate class, perhaps “MultiTimer” or something, which could manage an internal list of Timers?

I like the idea of a new MultiTimer for this purpose.

BTW, how do you manage to process multiple timer in current design ?

Ok, it’s quite a simple class, I’ll try to throw one together if I have a moment.

I don’t often seem to need more than one timer, but if I do, I’d just use more than one object. I see your point though, it would be handy sometimes.

Ok, do you want to give these a go - I’ve not got any code that would use it, so haven’t tried them yet:

[code]//==============================================================================
/**
A type of timer class that can run multiple timers with different frequencies,
all of which share a single callback.

This class is very similar to the Timer class, but allows you run multiple 
separate timers, where each one has a unique ID number. The methods in this
class are exactly equivalent to those in Timer, but with the addition of
this ID number.

To use it, you need to create a subclass of MultiTimer, implementing the 
timerCallback() method. Then you can start timers with startTimer(), and
each time the callback is triggered, it passes in the ID of the timer that
caused it.

@see Timer

/
class JUCE_API MultiTimer
{
protected:
//==============================================================================
/
* Creates a MultiTimer.

    When created, no timers are running, so use startTimer() to start things off.
*/
MultiTimer() throw();

/** Creates a copy of another timer.

    Note that this timer will not contain any running timers, even if the one you're 
    copying from was running.
*/
MultiTimer (const MultiTimer& other) throw();

public:
//==============================================================================
/** Destructor. */
virtual ~MultiTimer();

//==============================================================================
/** The user-defined callback routine that actually gets called periodically.

    It's perfectly ok to call startTimer() or stopTimer() from within this
    callback to change the subsequent intervals.
*/
virtual void timerCallback (const int timerId) = 0;

//==============================================================================
/** Starts a timer and sets the length of interval required.

    If the timer is already started, this will reset it, so the
    time between calling this method and the next timer callback
    will not be less than the interval length passed in.

    @param timerId                  a unique Id number that identifies the timer to
                                    start. This is the id that will be passed back 
                                    to the timerCallback() method when this timer is 
                                    triggered
    @param  intervalInMilliseconds  the interval to use (any values less than 1 will be
                                    rounded up to 1)
*/
void startTimer (const int timerId, const int intervalInMilliseconds) throw();

/** Stops a timer.

    If a timer has been started with the given ID number, it will be cancelled.
    No more callbacks will be made for the specified timer after this method returns.

    If this is called from a different thread, any callbacks that may
    be currently executing may be allowed to finish before the method
    returns.
*/
void stopTimer (const int timerId) throw();

//==============================================================================
/** Checks whether a timer has been started for a specified ID.

    @returns true if a timer with the given ID is running.
*/
bool isTimerRunning (const int timerId) const throw();

/** Returns the interval for a specified timer ID.

    @returns    the timer's interval in milliseconds if it's running, or 0 if it's no timer
                is running for the ID number specified.
*/
int getTimerInterval (const int timerId) const throw();


//==============================================================================

private:
CriticalSection timerListLock;
VoidArray timers;

const MultiTimer& operator= (const MultiTimer&);

};[/code]

//==============================================================================
class InternalMultiTimerCallback    : public Timer
{
public:
    InternalMultiTimerCallback (const int id_, MultiTimer& owner_)
        : id (id_),
          owner (owner_)
    {
    }

    ~InternalMultiTimerCallback()
    {
    }

    void timerCallback()
    {
        owner.timerCallback (id);
    }

    const int id;

private:
    MultiTimer& owner;
};

//==============================================================================
MultiTimer::MultiTimer() throw()
{
}

MultiTimer::MultiTimer (const MultiTimer&) throw()
{
}

MultiTimer::~MultiTimer()
{
    const ScopedLock sl (timerListLock);

    for (int i = timers.size(); --i >= 0;)
        delete (InternalMultiTimerCallback*) timers.getUnchecked(i);

    timers.clear();
}

//==============================================================================
void MultiTimer::startTimer (const int timerId, const int intervalInMilliseconds) throw()
{
    const ScopedLock sl (timerListLock);

    for (int i = timers.size(); --i >= 0;)
    {
        InternalMultiTimerCallback* const t = (InternalMultiTimerCallback*) timers.getUnchecked(i);

        if (t->id == timerId)
        {
            t->startTimer (intervalInMilliseconds);
            return;
        }
    }

    InternalMultiTimerCallback* const newTimer = new InternalMultiTimerCallback (timerId, *this);
    timers.add (newTimer);
    newTimer->startTimer (intervalInMilliseconds);
}

void MultiTimer::stopTimer (const int timerId) throw()
{
    const ScopedLock sl (timerListLock);

    for (int i = timers.size(); --i >= 0;)
    {
        InternalMultiTimerCallback* const t = (InternalMultiTimerCallback*) timers.getUnchecked(i);

        if (t->id == timerId)
            t->stopTimer();
    }
}

bool MultiTimer::isTimerRunning (const int timerId) const throw()
{
    const ScopedLock sl (timerListLock);

    for (int i = timers.size(); --i >= 0;)
    {
        const InternalMultiTimerCallback* const t = (InternalMultiTimerCallback*) timers.getUnchecked(i);
        if (t->id == timerId)
            return t->isTimerRunning();
    }

    return false;
}

int MultiTimer::getTimerInterval (const int timerId) const throw()
{
    const ScopedLock sl (timerListLock);

    for (int i = timers.size(); --i >= 0;)
    {
        const InternalMultiTimerCallback* const t = (InternalMultiTimerCallback*) timers.getUnchecked(i);
        if (t->id == timerId)
            return t->getTimerInterval();
    }

    return 0;
}

Ok, it’s building and the solution is very clean

I wouldn’t have thought about separating Timer from Component base classes (due to my Windows handle/event way of thinking)… Great.

BTW, if I use a timer in a console based Juce project, is it working ?
I mean: What does this do:

            if (now <= lastTime)
            {
                wait (2);
                continue;
            }

and

                postMessage (new Message(), false);

I guess the first one prevent calling a timer callback just after starting it
But the second one require a OS message pump which might not be in a console app, right ?

[quote=“X-Ryl669”]Ok, it’s building and the solution is very clean

I wouldn’t have thought about separating Timer from Component base classes (due to my Windows handle/event way of thinking)… Great.

BTW, if I use a timer in a console based Juce project, is it working ?
I mean: What does this do:

            if (now <= lastTime)
            {
                wait (2);
                continue;
            }

and

                postMessage (new Message(), false);

I guess the first one prevent calling a timer callback just after starting it
But the second one require a OS message pump which might not be in a console app, right ?[/quote]

a console app can’t use timers because it’s got no event dispatch mechanism. You’re ok using threads and the wait() method though.