Wxruby and threads

Hello.
I trying to write a small GUI application.
I choose wxruby library.(any gui library with unicode is good for me)
My application use threads.
Next example is not work as expecting.

require 'thread'
require 'wx'
include Wx

class MinimalApp < App
  def on_init
    f=Frame.new(nil, -1,'Test')
    f.show
  end
end

Thread.new do
  loop {
    puts 'hello'
    STDOUT.flush
    sleep(1)
  }
end

MinimalApp.new.main_loop

After starting, 'hello' messages is show repeatly only when I drag the
window and
stops when I move the focus out.
What I missed ?

Regards,
Yuri Kozlov

Hello

Yuri Kozlov wrote:

Thread.new do
  loop {
    puts 'hello'
    STDOUT.flush
    sleep(1)
  }
end

MinimalApp.new.main_loop

I don't know exactly how ruby's threads interact with wx's scheduling. Perhaps you could try adding an evt_idle handler to your example?

For me this does what I'd expect: fire regularly and spawn and run threads fine. What I didn't expect, but that happened anyway, is that your original long-running Thread now also seems to run regularly regardless of app focus. Maybe add an explicit call to thread.run() in the on_idle handler?

alex

require 'thread'
require 'wx'

class MinimalApp < Wx::App
  def on_idle
    Thread.new do
      puts 'FOO'
      STDOUT.flush
      sleep(1)
    end
  end

  def on_init
    evt_idle { on_idle }
    f = Wx::Frame.new( nil, -1,'Test' )
    f.show
  end
end

$t = Thread.new do
  loop {
    puts 'BAR'
    STDOUT.flush
    sleep(1)
  }
end

MinimalApp.new.main_loop

Yuri Kozlov wrote:

What I missed ?

Ruby threads aren't system threads. When you're in the Wx main loop,
ruby isn't getting called, so your thread doesn't execute.

···

--
Neil Stevens - neil@hakubi.us

'A republic, if you can keep it.' -- Benjamin Franklin

It seems, what this code make new thread every time when on_idle
called.

class MinimalApp < Wx::App
def on_idle
   Thread.new do
     puts 'FOO'
     STDOUT.flush
     sleep(1)
   end
end

Anyway, its not worked for me.
Thank you, Alex.

Regards,
Yuri Kozlov

Ruby threads aren't system threads. When you're in the Wx main loop,
ruby isn't getting called, so your thread doesn't execute.

Ok. I suspected like this.

Is exists gui library worked with ruby threads?
Or I must fork the new process, doing interprocess communication ?

Regards,
Yuri Kozlov

Aha.
Tkruby is much better.
October 13, subject "threading in win32" by Ara.T.Howard

require "tk"

thread_runner = lambda do
   Thread::new do
       loop {
         puts 'hello'
         STDOUT.flush
         sleep(1)
       }
     end
   en

   TkButton::new(nil,
     "text" => "start thread/process",
     "command" => thread_runner).pack( "fill" =>"x")

   TkButton::new(nil,
     "text" => 'quit',
     "command" => lambda{exit}).pack("fill" => "x")

   Tk::mainloop

The problem is solved.

Regards,
Yuri Kozlov

Yuri Kozlov wrote:

It seems, what this code make new thread every time when on_idle
called.

Yes. It was just an example. If you want to run a previously defined thread, the following works for me (CVS HEAD, Wx 2.6.2 unicode, OS X 10.3, Ruby 1.8.2)

def on_idle
  @thread.run() end

a

Could you post the full text working example ?

Thank you.

Regards,
Yuri Kozlov

See below - note it doesn't really matter where you create the thread - it could be passed into the App's constructor. It's just in on_init for simplicity.

a

require 'thread'
require 'wx'

class MinimalApp < Wx::App
  def on_idle
    @thread.run()
  end

  def on_init
    @thread = Thread.new do
      loop {
        puts 'HELLO'
        STDOUT.flush
        sleep 1
      }
    end
    evt_idle { on_idle }
    f = Wx::Frame.new( nil, -1,'Test' )
    f.show
  end
end

MinimalApp.new().main_loop

Not worked.
linux-i686 ruby 1.8.2 wx2.6.2 wxruby2 cvs
windows ruby 1.8.2 wx2.4 wxruby old.

I will use Tk library.

Thank you, Alex.

Regards,
Yuri Kozlov