[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

angico

10/20/2008 8:28:00 PM

[Note: parts of this message were removed to make it a legal post.]

Hi, All.

Can anybody tell me how to implement kind of a timer in Ruby, similar to
the following one from Java?

timer = new Timer( 200, handler ); // call handler each 200 milliseconds


Thank you,

--

angico
------
home page: www.angico.org
Gnu/Linux, FLOSS, Espiritismo, e eu por mim mesmo 8^I
------
contatos:
email: angico@angico.org
skype: an.gi.co
------

3 Answers

Robert Klemme

10/20/2008 9:03:00 PM

0

On 20.10.2008 22:28, angico wrote:
> [Note: parts of this message were removed to make it a legal post.]
>
> Hi, All.
>
> Can anybody tell me how to implement kind of a timer in Ruby, similar to
> the following one from Java?
>
> timer = new Timer( 200, handler ); // call handler each 200 milliseconds

Here's a simple one

#!/bin/env ruby

require 'monitor'

class Timer
def initialize(interval, &handler)
raise ArgumentError, "Illegal interval" if interval < 0
extend MonitorMixin
@run = true
@th = Thread.new do
t = Time.now
while run?
t += interval
(sleep(t - Time.now) rescue nil) and
handler.call rescue nil
end
end
end

def stop
synchronize do
@run = false
end
@th.join
end

private

def run?
synchronize do
@run
end
end
end

t = Timer.new(1) do
puts "tick #{Time.now.to_f}"
# random sleep to show slot stability
sleep((5 + rand(10))/10.0)
end

sleep 10
t.stop

Brian Candler

10/20/2008 9:12:00 PM

0

angico wrote:
> Hi, All.
>
> Can anybody tell me how to implement kind of a timer in Ruby, similar to
> the following one from Java?
>
> timer = new Timer( 200, handler ); // call handler each 200 milliseconds

If your handler is short and you don't care too much about accuracy:

Thread.new { loop { handler; sleep 0.2 } }
--
Posted via http://www.ruby-....

Joel VanderWerf

10/20/2008 9:35:00 PM

0

angico wrote:
> Hi, All.
>
> Can anybody tell me how to implement kind of a timer in Ruby, similar to
> the following one from Java?
>
> timer = new Timer( 200, handler ); // call handler each 200 milliseconds

This is what I use:

http://redshift.sourceforge.net/time...

It's pretty simple, and it corrects for the time taken by the handler.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407