From: lover ruby Date: 2011-10-11T15:08:03+09:00 Subject: Re: scheduler and thread Sam Duncan wrote in post #1025958: > On 11/10/11 11:20, lover ruby wrote: >> sleep([interval - elapsed, 0].max) >> --------------------------------------------------- >> But it never prints out "Doing other stuff..." >> >> How can I make it print out like >> "Doing other stuff..." >> 1318284723 >> 1318284728 >> 1318284733 >> >> Thanks > Try this; > > module Timer > > @timer = nil > > def Timer.repeat_every(interval) > @timer = Thread.new do > loop do > start_time = Time.now > yield > elapsed = Time.now - start_time > sleep([interval - elapsed, 0].max) > end > end > end > > def Timer.stop > unless @timer.nil? > @timer.kill > @timer.join(1) # don't hang forever > @timer = nil > end > end > > end > > Timer.repeat_every(5) do > puts Time.now.to_i > end > > puts "Doing other stuff..." > sleep 10 > puts "Stopping timer" > Timer.stop > > > As far as I can tell your execution is hanging on the join which will > never return as the timer thread is in an infinite loop =] > > Hope that helps > Sam Thanks for help. The result is 1318313034Doing other stuff... 1318313039 And stop. Even I remove Timer.stop. I want something like UI.start_timer in Google sketchup. After UI.start_timer, every code is executable. So the UI.start_timer acts like a background process. -- Posted via http://www.ruby-forum.com/.