Paul Brannan
4/27/2007 1:23:00 PM
On Fri, Apr 27, 2007 at 04:19:18PM +0900, Hussain wrote:
> Hai Dude
>
> I want to write one class that class should act as a Timer
> On that class , i will check the database every second for database
> field called Diaplay_time ,if the current system time matched with
> database Display_Time field data then i will show the message in
> console,That class should terminate only when i press "Ctrl+C" key
> otherwise that wont stop
> ok
> can any body tell me the solution?
> I need to do that
In general there are two common approaches:
- use threads
- use an event loop
In Ruby, threads are easy to write. To set up a timer using threads,
create a thread that sleeps for one second, wakes up, then does your
check. The disadvantage to this approach is that your code must be
thread-safe.
With an event loop, your code waits for an event (a timer to fire, input
from the user, etc.) and responds to that event. There are numerous
libraries out there to help with this, including Ruby/Event and
IO::Reactor.
Paul