| Version: 3.0.2

- Main Page
- Related Pages
- Categories
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | List of all members
wxTimer Class Reference
Miscellaneous

`#include <wx/timer.h>`

Inheritance diagram for wxTimer:
[legend]

## Detailed Description

The wxTimer class allows you to execute code at specified intervals.

Its precision is platform-dependent, but in general will not be better than `1ms` nor worse than `1s`.

There are three different ways to use this class:

- You may derive a new class from wxTimer and override the wxTimer::Notify member to perform the required action.
- You may redirect the notifications to any wxEvtHandler derived object by using the non-default constructor or wxTimer::SetOwner. Then use the `EVT_TIMER` macro to connect it to the event handler which will receive wxTimerEvent notifications.
- You may use a derived class and the `EVT_TIMER` macro to connect it to an event handler defined in the derived class. If the default constructor is used, the timer object will be its own owner object, since it is derived from wxEvtHandler.

In any case, you must start the timer with wxTimer::Start() after constructing it before it actually starts sending notifications. It can be stopped later with wxTimer::Stop().

Note
  A timer can only be used from the main thread.

##

Library: wxBase
Category: Miscellaneous

See Also
  wxStopWatch

## Public Member Functions
| wxTimer ()
| Default constructor.
| wxTimer (wxEvtHandler *owner, int id=-1)
| Creates a timer and associates it with owner.
virtual | ~wxTimer ()
| Destructor.
int | GetId () const
| Returns the ID of the events generated by this timer.
int | GetInterval () const
| Returns the current interval for the timer (in milliseconds).
wxEvtHandler * | GetOwner () const
| Returns the current owner of the timer.
bool | IsOneShot () const
| Returns true if the timer is one shot, i.e. if it will stop after firing the first notification automatically.
bool | IsRunning () const
| Returns true if the timer is running, false if it is stopped.
virtual void | Notify ()
| This member should be overridden by the user if the default constructor was used and SetOwner() wasn't called.
void | SetOwner (wxEvtHandler *owner, int id=-1)
| Associates the timer with the given owner object.
virtual bool | Start (int milliseconds=-1, bool oneShot=wxTIMER_CONTINUOUS)
| (Re)starts the timer.
bool | StartOnce (int milliseconds=-1)
| Starts the timer for a once-only notification.
virtual void | Stop ()
| Stops the timer.
Public Member Functions inherited from wxEvtHandler
| wxEvtHandler ()
| Constructor.
virtual | ~wxEvtHandler ()
| Destructor.
virtual void | QueueEvent (wxEvent *event)
| Queue event for a later processing.
virtual void | AddPendingEvent (const wxEvent &event)
| Post an event to be processed later.
template<typename T , typename T1 , ... >
void | CallAfter (void(T::*method)(T1,...), T1 x1,...)
| Asynchronously call the given method.
template<typename T >
void | CallAfter (const T &functor)
| Asynchronously call the given functor.
virtual bool | ProcessEvent (wxEvent &event)
| Processes an event, searching event tables and calling zero or more suitable event handler function(s).
bool | ProcessEventLocally (wxEvent &event)
| Try to process the event in this handler and all those chained to it.
bool | SafelyProcessEvent (wxEvent &event)
| Processes an event by calling ProcessEvent() and handles any exceptions that occur in the process.
void | ProcessPendingEvents ()
| Processes the pending events previously queued using QueueEvent() or AddPendingEvent(); you must call this function only if you are sure there are pending events for this handler, otherwise a `wxCHECK` will fail.
void | DeletePendingEvents ()
| Deletes all events queued on this event handler using QueueEvent() or AddPendingEvent().
virtual bool | SearchEventTable (wxEventTable &table, wxEvent &event)
| Searches the event table, executing an event handler function if an appropriate one is found.
void | Connect (int id, int lastId, wxEventType eventType, wxObjectEventFunction function, wxObject *userData=NULL, wxEvtHandler *eventSink=NULL)
| Connects the given function dynamically with the event handler, id and event type.
void | Connect (int id, wxEventType eventType, wxObjectEventFunction function, wxObject *userData=NULL, wxEvtHandler *eventSink=NULL)
| See the Connect(int, int, wxEventType, wxObjectEventFunction, wxObject*, wxEvtHandler*) overload for more info.
void | Connect (wxEventType eventType, wxObjectEventFunction function, wxObject *userData=NULL, wxEvtHandler *eventSink=NULL)
| See the Connect(int, int, wxEventType, wxObjectEventFunction, wxObject*, wxEvtHandler*) overload for more info.
bool | Disconnect (wxEventType eventType, wxObjectEventFunction function, wxObject *userData=NULL, wxEvtHandler *eventSink=NULL)
| Disconnects the given function dynamically from the event handler, using the specified parameters as search criteria and returning true if a matching function has been found and removed.
bool | Disconnect (int id=wxID_ANY, wxEventType eventType=wxEVT_NULL, wxObjectEventFunction function=NULL, wxObject *userData=NULL, wxEvtHandler *eventSink=NULL)
| See the Disconnect(wxEventType, wxObjectEventFunction, wxObject*, wxEvtHandler*) overload for more info.
bool | Disconnect (int id, int lastId, wxEventType eventType, wxObjectEventFunction function=NULL, wxObject *userData=NULL, wxEvtHandler *eventSink=NULL)
| See the Disconnect(wxEventType, wxObjectEventFunction, wxObject*, wxEvtHandler*) overload for more info.
template<typename EventTag , typename Functor >
void | Bind (const EventTag &eventType, Functor functor, int id=wxID_ANY, int lastId=wxID_ANY, wxObject *userData=NULL)
| Binds the given function, functor or method dynamically with the event.
template<typename EventTag , typename Class , typename EventArg , typename EventHandler >
void | Bind (const EventTag &eventType, void(Class::*method)(EventArg &), EventHandler *handler, int id=wxID_ANY, int lastId=wxID_ANY, wxObject *userData=NULL)
| See the Bind<>(const EventTag&, Functor, int, int, wxObject*) overload for more info.
template<typename EventTag , typename Functor >
bool | Unbind (const EventTag &eventType, Functor functor, int id=wxID_ANY, int lastId=wxID_ANY, wxObject *userData=NULL)
| Unbinds the given function, functor or method dynamically from the event handler, using the specified parameters as search criteria and returning true if a matching function has been found and removed.
template<typename EventTag , typename Class , typename EventArg , typename EventHandler >
bool | Unbind (const EventTag &eventType, void(Class::*method)(EventArg &), EventHandler *handler, int id=wxID_ANY, int lastId=wxID_ANY, wxObject *userData=NULL)
| See the Unbind<>(const EventTag&, Functor, int, int, wxObject*) overload for more info.
void * | GetClientData () const
| Returns user-supplied client data.
wxClientData * | GetClientObject () const
| Returns a pointer to the user-supplied client data object.
void | SetClientData (void *data)
| Sets user-supplied client data.
void | SetClientObject (wxClientData *data)
| Set the client data object.
bool | GetEvtHandlerEnabled () const
| Returns true if the event handler is enabled, false otherwise.
wxEvtHandler * | GetNextHandler () const
| Returns the pointer to the next handler in the chain.
wxEvtHandler * | GetPreviousHandler () const
| Returns the pointer to the previous handler in the chain.
void | SetEvtHandlerEnabled (bool enabled)
| Enables or disables the event handler.
virtual void | SetNextHandler (wxEvtHandler *handler)
| Sets the pointer to the next handler.
virtual void | SetPreviousHandler (wxEvtHandler *handler)
| Sets the pointer to the previous handler.
void | Unlink ()
| Unlinks this event handler from the chain it's part of (if any); then links the "previous" event handler to the "next" one (so that the chain won't be interrupted).
bool | IsUnlinked () const
| Returns true if the next and the previous handler pointers of this event handler instance are NULL.
Public Member Functions inherited from wxObject
| wxObject ()
| Default ctor; initializes to NULL the internal reference data.
| wxObject (const wxObject &other)
| Copy ctor.
virtual | ~wxObject ()
| Destructor.
virtual wxClassInfo * | GetClassInfo () const
| This virtual function is redefined for every class that requires run-time type information, when using the wxDECLARE_CLASS macro (or similar).
wxObjectRefData * | GetRefData () const
| Returns the wxObject::m_refData pointer, i.e. the data referenced by this object.
bool | IsKindOf (const wxClassInfo *info) const
| Determines whether this class is a subclass of (or the same class as) the given class.
bool | IsSameAs (const wxObject &obj) const
| Returns true if this object has the same data pointer as obj.
void | Ref (const wxObject &clone)
| Makes this object refer to the data in clone.
void | SetRefData (wxObjectRefData *data)
| Sets the wxObject::m_refData pointer.
void | UnRef ()
| Decrements the reference count in the associated data, and if it is zero, deletes the data.
void | UnShare ()
| This is the same of AllocExclusive() but this method is public.
void | operator delete (void *buf)
| The delete operator is defined for debugging versions of the library only, when the identifier `WXDEBUG` is defined.
void * | operator new (size_t size, const wxString &filename=NULL, int lineNum=0)
| The new operator is defined for debugging versions of the library only, when the identifier `WXDEBUG` is defined.

## Additional Inherited Members
Static Public Member Functions inherited from wxEvtHandler
static void | AddFilter (wxEventFilter *filter)
| Add an event filter whose FilterEvent() method will be called for each and every event processed by wxWidgets.
static void | RemoveFilter (wxEventFilter *filter)
| Remove a filter previously installed with AddFilter().
Protected Member Functions inherited from wxEvtHandler
virtual bool | TryBefore (wxEvent &event)
| Method called by ProcessEvent() before examining this object event tables.
virtual bool | TryAfter (wxEvent &event)
| Method called by ProcessEvent() as last resort.
Protected Attributes inherited from wxObject
wxObjectRefData * | m_refData
| Pointer to an object which is the object's reference-counted data.

## Constructor & Destructor Documentation

wxTimer::wxTimer | ( |  | ) |

Default constructor.

If you use it to construct the object and don't call SetOwner() later, you must override Notify() method to process the notifications.

wxTimer::wxTimer | ( | wxEvtHandler * | owner,
|  | int | id = `-1`
| ) |  |

Creates a timer and associates it with owner.

Please see SetOwner() for the description of parameters.

virtual wxTimer::~wxTimer | ( |  | ) |
| virtual

Destructor.

Stops the timer if it is running.

## Member Function Documentation

int wxTimer::GetId | ( |  | ) | const

Returns the ID of the events generated by this timer.

int wxTimer::GetInterval | ( |  | ) | const

Returns the current interval for the timer (in milliseconds).

wxEvtHandler* wxTimer::GetOwner | ( |  | ) | const

Returns the current owner of the timer.

If non-NULL this is the event handler which will receive the timer events (see wxTimerEvent) when the timer is running.

bool wxTimer::IsOneShot | ( |  | ) | const

Returns true if the timer is one shot, i.e. if it will stop after firing the first notification automatically.

bool wxTimer::IsRunning | ( |  | ) | const

Returns true if the timer is running, false if it is stopped.

virtual void wxTimer::Notify | ( |  | ) |
| virtual

This member should be overridden by the user if the default constructor was used and SetOwner() wasn't called.

Perform whatever action which is to be taken periodically here.

Notice that throwing exceptions from this method is currently not supported, use event-based timer handling approach if an exception can be thrown while handling timer notifications.

void wxTimer::SetOwner | ( | wxEvtHandler * | owner,
|  | int | id = `-1`
| ) |  |

Associates the timer with the given owner object.

When the timer is running, the owner will receive timer events (see wxTimerEvent) with id equal to id specified here.

virtual bool wxTimer::Start | ( | int | milliseconds = `-1`,
|  | bool | oneShot = `wxTIMER_CONTINUOUS`
| ) |  |
| virtual

(Re)starts the timer.

If milliseconds parameter is -1 (value by default), the previous value is used. Returns false if the timer could not be started, true otherwise (in MS Windows timers are a limited resource).

If oneShot is false (the default), the Notify() function will be called repeatedly until the timer is stopped. If true, it will be called only once and the timer will stop automatically.

To make your code more readable you may also use the following symbolic constants:

- wxTIMER_CONTINUOUS: Start a normal, continuously running, timer
- wxTIMER_ONE_SHOT: Start a one shot timer Alternatively, use StartOnce().

If the timer was already running, it will be stopped by this method before restarting it.

bool wxTimer::StartOnce | ( | int | milliseconds = `-1` | ) |

Starts the timer for a once-only notification.

This is a simple wrapper for Start() with `wxTIMER_ONE_SHOT` parameter.

Since
  2.9.5

virtual void wxTimer::Stop | ( |  | ) |
| virtual

Stops the timer.