Hi,
I am using wxRuby 1.9.7 and Ruby 1.8.6. I have built an extension in C++
and used swig to load it into Ruby interpreter, which works great. The
extension is an often long executing algorithm, and I’ve noticed that if
I fork a new Ruby thread in button clicked event, and in this thread run
the algorithm the application freezes for the time of execution. I have
looked through the archive and found that in the wxRuby main thread I
need to use timer, but that does not work for the “algorithm thread”, I
dont know how to stop it or keep GUI responsive? Can anyone suggest what
should I do. I know about Green Threads, and possibly that interpreter
can not switch context when extension is running, so what should I do
maybe use a thread at the side of extension (in c++ code) or maybe try
to use the same mechanizm with timer as wxRuby does (but how to
implement that).
My code is simple:
require ‘wx’
require ‘cppapp’ #myextension
class MFrame < Wx::Frame
STEPS = 100
def initialize
super(nil,-1,“Title”)
set_client_size(Wx::Size.new(300,300))
panel = Wx::Panel.new(self)
sizer = Wx::BoxSizer.new(Wx::VERTICAL)
btn = Wx::Button.new(panel, -1, "Click me")
sizer.add(btn, 0, Wx::GROW|Wx::ALL,2)
evt_button(btn.get_id){
Thread.abort_on_exception = true
#that does not work
@th = Thread.new do
Wx::Timer.every(20) do
Thread.pass
end
method1
end
}
panel.set_sizer(sizer)
sizer.fit(panel)
end
def method1
alg = Cppapp::Algorithm.new
alg.form = Cppapp::Form.new
alg.algorithmize
end
end
class MApp < Wx::App
def on_init
MFrame.new.show
Wx::Timer.every(20) {Thread.pass}
end
end
MApp.new.main_loop