# File.read block in Thread

**URL:** <https://www.ruby-forum.com/t/file-read-block-in-thread/188330>\
**Category:** Ruby-Gnome 2\
**Created:** [May 10, 2010, 11:43am UTC](https://www.ruby-forum.com/t/file-read-block-in-thread/188330 "2010-05-10T11:43:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![miao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8baadc/32.png) [@miao](https://www.ruby-forum.com/u/miao)\
**Post date:** [May 10, 2010, 11:43am UTC](https://www.ruby-forum.com/t/file-read-block-in-thread/188330/1 "2010-05-10T11:43:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![miao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8baadc/32.png) [@miao](https://www.ruby-forum.com/u/miao)\
**Post date:** [May 10, 2010, 2:40pm UTC](https://www.ruby-forum.com/t/file-read-block-in-thread/188330/2 "2010-05-10T14:40:14Z")

</div>

On 10 May 2010 18:43, Miao J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

```

---

<div class="post-metadata">

**Author:** ![miao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8baadc/32.png) [@miao](https://www.ruby-forum.com/u/miao)\
**Post date:** [May 10, 2010, 5:57pm UTC](https://www.ruby-forum.com/t/file-read-block-in-thread/188330/3 "2010-05-10T17:57:44Z")

</div>

Mike C. wrote:

> On 10 May 2010 18:43, Miao J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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
