If you tried to run a wxRuby GUI application with other Ruby threads, you noticed that the wxRuby event loop is greedy, and renders the other threads very slow. Especially, if you do IO operations, file management in the other thread.
The problem is not wxRuby specific, you see the same behavior using other GUI toolkits – so I read it. The reason is that the Ruby thread scheduler can not interrupt external programs the same way it interrupts Ruby threads.
The suggested approach is to use a Wx::Timer in your main thread – the GUI thread – to periodically instruct the scheduler to activate other threads.
While various other sources suggest the following code
timer = Timer.new(self) evt_timer(timer.id) do Thread.pass end timer.start(100)
it was not optimal in my case – still very slow.
I tried to run the timer with a 1ms interval
timer = Timer.new(self) evt_timer(timer.id) do Thread.pass end timer.start(1)
still not good.
So – and here is what I suggest you do -, I ended up using a sleep in the timer event, and giving explicit, bigger time slots to my IO intensive thread.
timer = Timer.new(self) evt_timer(timer.id) do sleep 0.01 end timer.start(10)
The 0.01 (seconds sleep time) and the 10 (milliseconds interval) in my case are arbitrary numbers, you need to fine tune them based on your application and user experience. But, it helped big time for my application.
on September 12th, 2009 by admin