wxruby and threads

Y

Yuri Kozlov

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
 
A

Alex Fenton

Hello

Yuri said:
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
 
N

Neil Stevens

Yuri said:
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.
 
Y

Yuri Kozlov

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
 
Y

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
 
Y

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
 
Y

Yuri Kozlov

Could you post the full text working example ?

Thank you.

Regards,
Yuri Kozlov
 
A

Alex Fenton

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
 
Y

Yuri Kozlov

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top