[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

wxruby and threads

Yuri Kozlov

12/7/2005 10:56:00 AM

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

9 Answers

Alex Fenton

12/7/2005 12:06:00 PM

0

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 Stevens

12/7/2005 12:10:00 PM

0

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

Yuri Kozlov

12/7/2005 12:45:00 PM

0

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

Yuri Kozlov

12/7/2005 12:51:00 PM

0

>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

Alex Fenton

12/7/2005 1:12:00 PM

0

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

Yuri Kozlov

12/7/2005 1:12:00 PM

0

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

12/7/2005 1:25:00 PM

0

Could you post the full text working example ?

Thank you.

Regards,
Yuri Kozlov

Alex Fenton

12/7/2005 2:48:00 PM

0

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

Yuri Kozlov

12/8/2005 6:23:00 AM

0

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