Forum: Ruby wxruby and threads

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
kozlov.y (Guest)
on 2005-12-07 13:00
(Received via mailing list)
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
alex (Guest)
on 2005-12-07 14:10
(Received via mailing list)
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
neil (Guest)
on 2005-12-07 14:14
(Received via mailing list)
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 S. - removed_email_address@domain.invalid

'A republic, if you can keep it.' -- Benjamin Franklin
kozlov.y (Guest)
on 2005-12-07 14:51
(Received via mailing list)
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
kozlov.y (Guest)
on 2005-12-07 14:55
(Received via mailing list)
>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
kozlov.y (Guest)
on 2005-12-07 15:15
(Received via mailing list)
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
alex (Guest)
on 2005-12-07 15:15
(Received via mailing list)
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
kozlov.y (Guest)
on 2005-12-07 15:27
(Received via mailing list)
Could you post the full text working example ?

Thank you.

Regards,
Yuri Kozlov
alex (Guest)
on 2005-12-07 16:53
(Received via mailing list)
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
kozlov.y (Guest)
on 2005-12-08 08:28
(Received via mailing list)
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
This topic is locked and can not be replied to.