# Can't use open-uri in a Thread if there are treectrl

**URL:** <https://www.ruby-forum.com/t/cant-use-open-uri-in-a-thread-if-there-are-treectrl/228166>\
**Category:** wxRuby\
**Created:** [April 12, 2013, 4:07am UTC](https://www.ruby-forum.com/t/cant-use-open-uri-in-a-thread-if-there-are-treectrl/228166 "2013-04-12T04:07:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tetsuya\_YUASA](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b487fb/32.png) [@Tetsuya\_YUASA](https://www.ruby-forum.com/u/Tetsuya_YUASA)\
**Post date:** [April 12, 2013, 4:07am UTC](https://www.ruby-forum.com/t/cant-use-open-uri-in-a-thread-if-there-are-treectrl/228166/1 "2013-04-12T04:07:58Z")

</div>

I want to use Open-URI::open in a Thread.  
But with this code freeze the window when I push toolbar button.

require “open-uri”  
require ‘wx’  
class MyApp \< Wx::App  
def on\_init  
MyFrame.new.show  
end  
end

class MyFrame \< Wx::Frame  
def initialize  
super(nil, -1, “TreeCtrl,Thread,Open-URI”)  
toolbar = Wx::ToolBar.new(self)  
top\_sizer = Wx::BoxSizer.new(Wx::VERTICAL)  
treectrl = Wx::TreeCtrl.new(self)  
top\_sizer.add(treectrl)  
toolbar.add\_tool(Wx::ID\_OPEN, ‘Open’,  
Wx::ArtProvider.bitmap(Wx::ART\_GO\_HOME), ‘Open URI’)  
toolbar.realize  
evt\_tool(Wx::ID\_OPEN){|event| on\_open\_uri(event)}  
self.set\_tool\_bar(toolbar)  
Wx::Timer.every(100){ sleep 0.05 }  
end

def on\_open\_uri(event)  
Thread.new{  
open(“[http://google.com](http://google.com)”){|f|  
p content = f.read  
}  
}  
end  
end

MyApp.new.main\_loop

If I comment out following two lines, it works fine.

# treectrl = Wx::TreeCtrl.new(self)

# top\_sizer.add(treectrl)

And without “Thread” this program runs well.

I would like to add that if I don’t use Open::URI.open in the Thread, no  
error occurs.

This behavior seems strange to me.  
Why is this happening?  
How can I use Open-URI in a Thread if I want to use the TreeCtrl?

---

<div class="post-metadata">

**Author:** ![Tetsuya\_YUASA](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b487fb/32.png) [@Tetsuya\_YUASA](https://www.ruby-forum.com/u/Tetsuya_YUASA)\
**Post date:** [April 15, 2013, 9:29pm UTC](https://www.ruby-forum.com/t/cant-use-open-uri-in-a-thread-if-there-are-treectrl/228166/2 "2013-04-15T21:29:01Z")

</div>

Hello Tetsuya,

First of all, when dealing with Ruby Threads, you need to have a  
Thread.pass in your code, in order to allow Ruby’s Green Threads to  
operate, and get CPU time, outside of the Main GUI Thread.

You can do this, by putting in your MyApp#on\_init method:

class MyApp \< Wx::App  
def on\_init  
Wx::Timer.every(500) { Thread.pass }  
MyFrame.new.show  
end  
end

This will give you some more responsiveness with Green Threads, and  
OpenURI. The other thing you should look at, is OpenURI’s methods for  
callbacks options in Open, more specifically

> **[Module: OpenURI::OpenRead (Ruby 1.9.3)](http://ruby-doc.org/stdlib-1.9.3/libdoc/open-uri/rdoc/OpenURI/OpenRead.html)**
>
> Module : OpenURI::OpenRead - Ruby 1.9.3

With  
the options for :content\_length\_proc and :progress\_proc, these options  
will  
give you the ability to get call-backs after the header is received for  
how  
much is about to be downloaded with :content\_length\_proc, and as the  
download occurs, it will return the total downloaded bytes that OpenURI  
has  
received. This is generally useful for Progress Bars, and such.

hth,

Mario

On Thu, Apr 11, 2013 at 9:44 PM, Tetsuya YUASA [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> evt_tool(Wx::ID_OPEN){|event| on_open_uri(event)}
> 
> ```
> 
> end
> 
> * * *
> 
> wxruby-users mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/wxruby-users](http://rubyforge.org/mailman/listinfo/wxruby-users)

–  
Mario S.  
Fleet Captain  
CO - Geo 99  
CO - USS T’hy’la  
XO - Diplomatic Corps - Second Life  
[http://www.iftcommand.com/chapters/thyla/](http://www.iftcommand.com/chapters/thyla/)

> **[HOME | The Federation](https://www.trekfederation.com)**

---

<div class="post-metadata">

**Author:** ![Tetsuya\_YUASA](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b487fb/32.png) [@Tetsuya\_YUASA](https://www.ruby-forum.com/u/Tetsuya_YUASA)\
**Post date:** [April 16, 2013, 3:58am UTC](https://www.ruby-forum.com/t/cant-use-open-uri-in-a-thread-if-there-are-treectrl/228166/3 "2013-04-16T03:58:44Z")

</div>

Hello Mario,  
Thank you for your advice!

As you say, I added the following line into the script.  
Wx::Timer.every(500) { Thread.pass }

But the Window still freeze if I press the button.

In the last message, I forgot to write my environment.  
I’m using wxruby-ruby19 (2.0.1 x86-mingw32), and ruby 1.9.3p125  
(2012-02-16) [i386-mingw32]

Is there some other way to solve the problem?

–  
Tetsuya Yuasa
