Forum: wxRuby Can't use open-uri in a Thread if there are treectrl

2aad82720b49396fa84ba90edf53928e?d=identicon&s=25 Tetsuya YUASA (Guest)
on 2013-04-12 04:07
(Received via mailing list)
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

class MyFrame < Wx::Frame
  def initialize
    super(nil, -1, "TreeCtrl,Thread,Open-URI")
    toolbar =
    top_sizer =
    treectrl =
    toolbar.add_tool(Wx::ID_OPEN, 'Open',
Wx::ArtProvider.bitmap(Wx::ART_GO_HOME), 'Open URI')
    evt_tool(Wx::ID_OPEN){|event| on_open_uri(event)}
    Wx::Timer.every(100){ sleep 0.05 }

  def on_open_uri(event){
        p content =

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

# treectrl =
# top_sizer.add(treectrl)

And without "Thread" this program runs well.

I would like to add that if I don't use 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?
3396e4a3df8a840faec520af8555a400?d=identicon&s=25 Mario Steele (Guest)
on 2013-04-15 21:29
(Received via mailing list)
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 }

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
the options for :content_length_proc and :progress_proc, these options
give you the ability to get call-backs after the header is received for
much is about to be downloaded with :content_length_proc, and as the
download occurs, it will return the total downloaded bytes that OpenURI
received.  This is generally useful for Progress Bars, and such.



On Thu, Apr 11, 2013 at 9:44 PM, Tetsuya YUASA <>

>     evt_tool(Wx::ID_OPEN){|event| on_open_uri(event)}
>   end
> _______________________________________________
> wxruby-users mailing list

Mario Steele
Fleet Captain
CO - Geo 99
CO - USS T'hy'la
XO - Diplomatic Corps - Second Life
2aad82720b49396fa84ba90edf53928e?d=identicon&s=25 Tetsuya YUASA (Guest)
on 2013-04-16 03:58
(Received via mailing list)
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
This topic is locked and can not be replied to.