juce_Timer.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_TIMER_H_INCLUDED
  18. #define JUCE_TIMER_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. Makes repeated callbacks to a virtual method at a specified time interval.
  22. A Timer's timerCallback() method will be repeatedly called at a given
  23. interval. When you create a Timer object, it will do nothing until the
  24. startTimer() method is called, which will cause the message thread to
  25. start making callbacks at the specified interval, until stopTimer() is called
  26. or the object is deleted.
  27. The time interval isn't guaranteed to be precise to any more than maybe
  28. 10-20ms, and the intervals may end up being much longer than requested if the
  29. system is busy. Because the callbacks are made by the main message thread,
  30. anything that blocks the message queue for a period of time will also prevent
  31. any timers from running until it can carry on.
  32. If you need to have a single callback that is shared by multiple timers with
  33. different frequencies, then the MultiTimer class allows you to do that - its
  34. structure is very similar to the Timer class, but contains multiple timers
  35. internally, each one identified by an ID number.
  36. @see HighResolutionTimer, MultiTimer
  37. */
  38. class JUCE_API Timer
  39. {
  40. protected:
  41. //==============================================================================
  42. /** Creates a Timer.
  43. When created, the timer is stopped, so use startTimer() to get it going.
  44. */
  45. Timer() noexcept;
  46. /** Creates a copy of another timer.
  47. Note that this timer won't be started, even if the one you're copying
  48. is running.
  49. */
  50. Timer (const Timer&) noexcept;
  51. public:
  52. //==============================================================================
  53. /** Destructor. */
  54. virtual ~Timer();
  55. //==============================================================================
  56. /** The user-defined callback routine that actually gets called periodically.
  57. It's perfectly ok to call startTimer() or stopTimer() from within this
  58. callback to change the subsequent intervals.
  59. */
  60. virtual void timerCallback() = 0;
  61. //==============================================================================
  62. /** Starts the timer and sets the length of interval required.
  63. If the timer is already started, this will reset it, so the
  64. time between calling this method and the next timer callback
  65. will not be less than the interval length passed in.
  66. @param intervalInMilliseconds the interval to use (any value less
  67. than 1 will be rounded up to 1)
  68. */
  69. void startTimer (int intervalInMilliseconds) noexcept;
  70. /** Starts the timer with an interval specified in Hertz.
  71. This is effectively the same as calling startTimer (1000 / timerFrequencyHz).
  72. */
  73. void startTimerHz (int timerFrequencyHz) noexcept;
  74. /** Stops the timer.
  75. No more timer callbacks will be triggered after this method returns.
  76. Note that if you call this from a background thread while the message-thread
  77. is already in the middle of your callback, then this method will cancel any
  78. future timer callbacks, but it will return without waiting for the current one
  79. to finish. The current callback will continue, possibly still running some of
  80. your timer code after this method has returned.
  81. */
  82. void stopTimer() noexcept;
  83. //==============================================================================
  84. /** Returns true if the timer is currently running. */
  85. bool isTimerRunning() const noexcept { return timerPeriodMs > 0; }
  86. /** Returns the timer's interval.
  87. @returns the timer's interval in milliseconds if it's running, or 0 if it's not.
  88. */
  89. int getTimerInterval() const noexcept { return timerPeriodMs; }
  90. //==============================================================================
  91. /** For internal use only: invokes any timers that need callbacks.
  92. Don't call this unless you really know what you're doing!
  93. */
  94. static void JUCE_CALLTYPE callPendingTimersSynchronously();
  95. private:
  96. class TimerThread;
  97. friend class TimerThread;
  98. int timerCountdownMs, timerPeriodMs; // NB: these member variable names are a little verbose
  99. Timer* previousTimer, *nextTimer; // to reduce risk of name-clashes with user subclasses
  100. Timer& operator= (const Timer&) JUCE_DELETED_FUNCTION;
  101. };
  102. #endif // JUCE_TIMER_H_INCLUDED