Forum: Ruby-Gnome 2 File.read block in Thread

Posted by Miao Jiang (miao)
on 2010-05-10 11:43
Ruby Version: ruby 1.8.7 (2010-04-20 patchlevel 254) [i686-linux]
Ruby-Gnome 2 Version: ruby-gtk2-0.19.4.tar.gz

Code:
require 'rubygems'
require 'gtk2'

w=Gtk::Window.new
w.signal_connect('destroy') do
  Gtk.main_quit
end
w.show_all

Thread.new do
  while true
    puts File.read __FILE__ # blocked, if comment the line, the script
will print '---' every second as expect.
    puts '-'*80
    sleep 1
  end
end
Gtk.main

How to solve that?
Thanks
Posted by Mike Charlton (Guest)
on 2010-05-10 14:40
(Received via mailing list)
On 10 May 2010 18:43, Miao Jiang <ruby-forum-incoming@andreas-s.net> 
wrote:
> Ruby Version: ruby 1.8.7 (2010-04-20 patchlevel 254) [i686-linux]
> Ruby-Gnome 2 Version: ruby-gtk2-0.19.4.tar.gz

[...]

> How to solve that?

It works as expected for me.  That is, it prints out the file followed
by the line of dashes every second.  I'm using the packages in
Ubuntu 10.4 (lucid): Ruby 1.8.7 and ruby-gnome 19.3.

I got fooled at the beginning though because the file is as big as
my terminal.  So it prints out a new page and I don't see anything
unless I scroll back.

         MikeC
Posted by Miao Jiang (miao)
on 2010-05-10 17:57
Mike Charlton wrote:
> On 10 May 2010 18:43, Miao Jiang <ruby-forum-incoming@andreas-s.net> 
> wrote:
>> Ruby Version: ruby 1.8.7 (2010-04-20 patchlevel 254) [i686-linux]
>> Ruby-Gnome 2 Version: ruby-gtk2-0.19.4.tar.gz
> 
> [...]
> 
>> How to solve that?
> 
> It works as expected for me.  That is, it prints out the file followed
> by the line of dashes every second.  I'm using the packages in
> Ubuntu 10.4 (lucid): Ruby 1.8.7 and ruby-gnome 19.3.
> 
> I got fooled at the beginning though because the file is as big as
> my terminal.  So it prints out a new page and I don't see anything
> unless I scroll back.
> 
>          MikeC

Thank you for reply.
Quite strange, I still have no idea how to solve the Gtk.main or linux 
conflict(maybe), but it solved when I try Gtk.events_pending? and 
Gtk.main_iteration.

Patched code:
require 'rubygems'
require 'gtk2'

w=Gtk::Window.new
w.signal_connect('destroy') do
  $done=true
end
w.show_all

Thread.new do
  while true
    puts File.read __FILE__
    puts '-'*80
    sleep 1
  end
end

$done=false
begin
  while Gtk.events_pending?
    Gtk.main_iteration
  end
  sleep 0.02
end until $done

Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.