wxRuby Documentation Home

Wx::Timer

The Timer 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 several different ways to use this class:

The most convenient way is to use the Timer.every class method, which accepts
a Integer to specify the millisecond interval at which the Timer should
fire, and a block to specify what should happen at that interval. For
example, to print a message to STDOUT every 1/2 second:

Wx::Timer.every(500) do puts “Tick” end

Secondly you may use the event handling mechanism to direct events to
any object which inherits from EvtHandler. To do this
use the longer constructor or set_owner. Then use the
evt_timer method to connect it to the event handler which will receive
TimerEvent notifications.

  1. assuming ‘self’ is some Window or Wx::App
    timer = Wx::Timer.new(self)
    evt_timer(timer.id) { puts “Timer ticked” }
    timer.start(100)

Lastly, you may derive a new class from Timer and define your own
notify instance method in that to perform the
required action. For example:

class MyTimer < Wx::Timer def notify puts “Timer ticked” end end timer = MyTimer.new timer.start(100) # every 1/10 second

Unless you use Timer.every, you must start the timer with start
after constructing it before it actually starts sending notifications. It can
be stopped later with stop

Derived from

EvtHandler, Object

See also

::StartTimer, ::GetElapsedTime, StopWatch

Methods

Timer.new

Timer.new(%(arg-type)EvtHandler% owner, Integer id = -1)

Creates a timer and associates it with owner_. Please see
set_owner
setowner for the description of parameters.

Timer.after

Timer.after(%(arg-type)Integer% interval) { … }

Carries out the action specified in the passed block after interval
milliseconds have passed. Note that the action will happen only once; if
you want a repeating action, use Timer.every

If you wish to interrupt the timed event before it may have happened,
capture the return value which is the newly created timer for this
action, and call stop on it.

Timer.every

Timer.every(%(arg-type)Integer% interval) { … }

Repeats the action specified in the passed block every interval
milliseconds. The timer is owned by the application object and will fire
as long as the application is running.

The method returns the new timer; if you may wish to stop the timer
before the application finishes, you should capture this return value so
you can call stop on it later.

Timer#get_id

Integer get_id()

Returns the id of the timer.

Timer#get_interval

Integer get_interval()

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

Timer#is_one_shot

Boolean is_one_shot()

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

Timer#is_running

Boolean is_running()

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

Timer#notify

notify()

This member should be overridden by the user if the default constructor was
used and set_owner wasn’t called.

Perform whatever action which is to be taken periodically here.

Timer#set_owner

set_owner(%(arg-type)EvtHandler% owner, Integer id = -1)

Associates the timer with the given owner\/ object. When the timer is
running, the owner will receive timer events with
id equal to id\/ specified here.

Timer#start

Boolean start(%(arg-type)Integer% milliseconds = -1, Boolean oneShot = false)

(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:

TIMER_CONTINUOUS Start a normal, continuously running, timer
TIMER_ONE_SHOT Start a one shot timer

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

Timer#stop

stop()

Stops the timer.

[This page automatically generated from the Textile source at 2023-06-03 08:07:35 +0000]