I have a WxRuby application that takes a few inputs from the user and uses that information to search through a large file for matches. The application works very well, with one exception. If I click anywhere in the frame while the application is searching through the long file, the application hangs. Has anyone experienced anything similar to this? Any suggestions on how I can fix this? Thanks Jayson
Jayson, This was talked about a few months ago, here are the links to the conversations. http://rubyforge.org/pipermail/wxruby-users/2007-May/003064.html The code you want is in this thread http://rubyforge.org/pipermail/wxruby-users/2007-June/003152.html Welcome to wxRuby Sean On 10/3/07, Jayson Williams <williams.jayson at gmail.com> wrote:> I have a WxRuby application that takes a few inputs from the user and > uses that information to search through a large file for matches. The > application works very well, with one exception. If I click anywhere > in the frame while the application is searching through the long file, > the application hangs. Has anyone experienced anything similar to > this? Any suggestions on how I can fix this? > > Thanks > Jayson > _______________________________________________ > wxruby-users mailing list > wxruby-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/wxruby-users >
Thanks for the assist. Unfortunately this didnt work for me. I added the code to my on_init method, but no change in the UI hang. Here is my on_init (some lines left out for brevity). Would you mind having a look? def on_init t = Wx::Timer.new(self, 55) evt_timer(55) { Thread.pass } t.start(100) buildElements updateStatus('''',:setup) updateStatus(status_a,:and) updateStatus(status_b,:bnd) evt_button(SUBMIT_ID)do |event| formPassFlag = checkForm status = (formPassFlag == true) ? ''Processing...'' : ''Form Error'' updateStatus(status,:and) if formPassFlag == true @@submit_bt.enable(false) threads = [] threads << Thread.new(){getErrors} threads.each{|thread|thread.join} end On 10/4/07, Sean Long <sean.m.long at gmail.com> wrote:> Jayson, > > This was talked about a few months ago, here are the links to the conversations. > > http://rubyforge.org/pipermail/wxruby-users/2007-May/003064.html > > The code you want is in this thread > http://rubyforge.org/pipermail/wxruby-users/2007-June/003152.html > > Welcome to wxRuby > > Sean > > On 10/3/07, Jayson Williams <williams.jayson at gmail.com> wrote: > > I have a WxRuby application that takes a few inputs from the user and > > uses that information to search through a large file for matches. The > > application works very well, with one exception. If I click anywhere > > in the frame while the application is searching through the long file, > > the application hangs. Has anyone experienced anything similar to > > this? Any suggestions on how I can fix this? > > > > Thanks > > Jayson > > _______________________________________________ > > wxruby-users mailing list > > wxruby-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/wxruby-users > > > _______________________________________________ > wxruby-users mailing list > wxruby-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/wxruby-users >
Jayson, I have not used this technique myself so I may be of little help, have you tried changing the value passed to t.start? Also try a different value for the ID of the timer, Wx::ID_HIGHEST + 1 will be guaranteed not to be used elsewhere. Sean On 10/4/07, Jayson Williams <williams.jayson at gmail.com> wrote:> Thanks for the assist. Unfortunately this didnt work for me. I added > the code to my on_init method, but no change in the UI hang. Here is > my on_init (some lines left out for brevity). Would you mind having a > look? > > def on_init > t = Wx::Timer.new(self, 55) > evt_timer(55) { Thread.pass } > t.start(100) > > buildElements > updateStatus('''',:setup) > updateStatus(status_a,:and) > updateStatus(status_b,:bnd) > evt_button(SUBMIT_ID)do |event| > formPassFlag = checkForm > status = (formPassFlag == true) ? ''Processing...'' : ''Form Error'' > updateStatus(status,:and) > if formPassFlag == true > @@submit_bt.enable(false) > threads = [] > threads << Thread.new(){getErrors} > threads.each{|thread|thread.join} > end > > > On 10/4/07, Sean Long <sean.m.long at gmail.com> wrote: > > Jayson, > > > > This was talked about a few months ago, here are the links to the conversations. > > > > http://rubyforge.org/pipermail/wxruby-users/2007-May/003064.html > > > > The code you want is in this thread > > http://rubyforge.org/pipermail/wxruby-users/2007-June/003152.html > > > > Welcome to wxRuby > > > > Sean > > > > On 10/3/07, Jayson Williams <williams.jayson at gmail.com> wrote: > > > I have a WxRuby application that takes a few inputs from the user and > > > uses that information to search through a large file for matches. The > > > application works very well, with one exception. If I click anywhere > > > in the frame while the application is searching through the long file, > > > the application hangs. Has anyone experienced anything similar to > > > this? Any suggestions on how I can fix this? > > > > > > Thanks > > > Jayson > > > _______________________________________________ > > > wxruby-users mailing list > > > wxruby-users at rubyforge.org > > > http://rubyforge.org/mailman/listinfo/wxruby-users > > > > > _______________________________________________ > > wxruby-users mailing list > > wxruby-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/wxruby-users > > > _______________________________________________ > wxruby-users mailing list > wxruby-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/wxruby-users >
Maybe a bit OT but I have a question related to threads & wxRuby: Will threads be fixed in the next stable release of wxRuby? Multithreading is very useful and it should be possible to use it with wxRuby in a normal way :) Actually I''m writing an application which needs to download some data from the internet (about 300-400 html files). It takes some time so I have to display a progress bar. I must have a separate thread for downloading files because if I try to do everything in the main thread the UI is not responsive and the progress bar doesn''t update until I finish downloading. I have searched the archives of this mailing list and found this strange workaround with Wx::Timer. It works but to some extent - for some reason Net::HTTP#read_timeout doesn''t work so if the connection is interrupted the download never ends (although if I use it in a ruby script which doesn''t use wxRuby it works perfectly - I think it uses threads in a way wxRuby doesn''t like). I didn''t find a workaround yet. Maybe someone will fix multithreading in wxRuby? I don''t know the internals of wxRuby so I don''t know how easy/difficult it is to fix it. But I think that it would improve wxRuby greatly. For instance - downloading data and showing a progress bar is quite a typical task - it should be fairly straightforward to code something like this. Sorry for a long post. Other than that - wxRuby is great :) -- Jacek Nowak jacekwiktor at gmail.com
What is wrong with multi-threading in wx::ruby. Isn''t the Thread class a ruby core class? On 10/4/07, Jacek Nowak <jacekwiktor at gmail.com> wrote:> Maybe a bit OT but I have a question related to threads & wxRuby: > > Will threads be fixed in the next stable release of wxRuby? > > Multithreading is very useful and it should be possible to use it with > wxRuby in a normal way :) > > Actually I''m writing an application which needs to download some data > from the internet (about 300-400 html files). It takes some time so I > have to display a progress bar. I must have a separate thread for > downloading files because if I try to do everything in the main thread > the UI is not responsive and the progress bar doesn''t update until I > finish downloading. I have searched the archives of this mailing list > and found this strange workaround with Wx::Timer. It works but to some > extent - for some reason Net::HTTP#read_timeout doesn''t work so if the > connection is interrupted the download never ends (although if I use > it in a ruby script which doesn''t use wxRuby it works perfectly - I > think it uses threads in a way wxRuby doesn''t like). > I didn''t find a workaround yet. > > Maybe someone will fix multithreading in wxRuby? I don''t know the > internals of wxRuby so I don''t know how easy/difficult it is to fix > it. But I think that it would improve wxRuby greatly. > For instance - downloading data and showing a progress bar is quite a > typical task - it should be fairly straightforward to code something > like this. > > Sorry for a long post. > > Other than that - wxRuby is great :) > > -- > Jacek Nowak > jacekwiktor at gmail.com > _______________________________________________ > wxruby-users mailing list > wxruby-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/wxruby-users >
Jayson Williams wrote:> Thanks for the assist. Unfortunately this didnt work for me. I added > the code to my on_init method, but no change in the UI hang. Here is > my on_init (some lines left out for brevity). Would you mind having a > look? > > def on_init > t = Wx::Timer.new(self, 55) > evt_timer(55) { Thread.pass } > t.start(100) > > buildElements > updateStatus('''',:setup) > updateStatus(status_a,:and) > updateStatus(status_b,:bnd) > evt_button(SUBMIT_ID)do |event| > formPassFlag = checkForm > status = (formPassFlag == true) ? ''Processing...'' : ''Form Error'' > updateStatus(status,:and) > if formPassFlag == true > @@submit_bt.enable(false) > threads = [] > threads << Thread.new(){getErrors} > threads.each{|thread|thread.join} > end >The problem with this, is that your joining the thread. Once you create the thread, you don''t need to join it, Thread.pass will pass execution to it, so that it can run, before it goes to the next one. When you do thread.join, it''ll actually join that thread, and it becomes the main thread. So my suggestion to you, is to take out threads.each{|thread|thread.join} from there, and it should work, and allow the UI to update. Review my Tutorial at http://wxruby.rubyforge.org/wiki/wiki.pl?Sockets It''s geared towards Socket clients, but also shows how to create Threaded Applications. Any questions, feel free to email me. Mario Steele
Jacek Nowak wrote:> Maybe a bit OT but I have a question related to threads & wxRuby: > > Will threads be fixed in the next stable release of wxRuby? > > Multithreading is very useful and it should be possible to use it with > wxRuby in a normal way :) > > Actually I''m writing an application which needs to download some data > from the internet (about 300-400 html files). It takes some time so I > have to display a progress bar. I must have a separate thread for > downloading files because if I try to do everything in the main thread > the UI is not responsive and the progress bar doesn''t update until I > finish downloading. I have searched the archives of this mailing list > and found this strange workaround with Wx::Timer. It works but to some > extent - for some reason Net::HTTP#read_timeout doesn''t work so if the > connection is interrupted the download never ends (although if I use > it in a ruby script which doesn''t use wxRuby it works perfectly - I > think it uses threads in a way wxRuby doesn''t like). > I didn''t find a workaround yet. > > Maybe someone will fix multithreading in wxRuby? I don''t know the > internals of wxRuby so I don''t know how easy/difficult it is to fix > it. But I think that it would improve wxRuby greatly. > For instance - downloading data and showing a progress bar is quite a > typical task - it should be fairly straightforward to code something > like this. > > Sorry for a long post. > > Other than that - wxRuby is great :) >Hey Jacek, As I''ve just posted, my suggestion is to look at my tutorial about writing Networking Clients. You can find it: http://wxruby.rubyforge.org/wiki/wiki.pl?Sockets It gives you a good idea of how to allow threads to work with Network Communications. Any questions, feel free to email me, Mario Steele
Lance Carlson wrote:> What is wrong with multi-threading in wx::ruby. Isn''t the Thread class > a ruby core class? >Hey Lance, There is a problem with Multi-Threading that you may not realize, Ruby uses Green Threads, which means, that Ruby is the one in control of all the threads. It contains the code that actually creates a "Virtual Thread", that only the Ruby Interpreter knows about. wxRuby, or rather wxWidgets in general, uses OS Thread creation. wxRuby currently does not implement the wxThread class, cause the Ruby interpreter (Atleast as of 1.8 series) is not Thread compliant. But there are ways around that, and I''ve posted a tutorial, as previously mentioned twice now, that shows a work around, that will allow you to work with wxRuby and Ruby''s green threads. But yea, there are problems, and work arounds to make some things work. L8ers, Mario Steele
A bit late to this discussion, but ... Jacek Nowak wrote:> Will threads be fixed in the next stable release of wxRuby? >I don''t think there is a canonical fix. Most of the GUI toolkits suffer from the same problem (see http://www.infoq.com/news/2007/04/ruby-userspace-threads-roundup). Some (eg FxRuby) build-in a similar Timer-based workaround to the default App clas, but I think my preference is to let users who want to use Threads choose how to schedule the threads using Wx::Timer (it might vary depending on the need for timing accuracy, parallelism etc). Part of the problem is documenting this properly, so I added an example on using threads to the samples for the next release: http://wxruby.rubyforge.org/svn/trunk/wxruby2/samples/etc/threaded.rb> I have searched the archives of this mailing list > and found this strange workaround with Wx::Timer.Though it seems a bit strange, it''s very similar to the way Ruby''s threads scheduler works. It checks every 10ms (I think) to see if it''s in a place where it can switch context to another thread, and then does so. The problem with GUI toolkits is that Ruby''s scheduler can''t interrupt if the main thread is in third-party code, like the wxRuby library. There, most of the time it''s sitting inside the compiled code''s event loop, waiting for something to happen. So we need to use a Wx::Timer to emulate the scheduler and force a switch of Thread context.> It works but to some > extent - for some reason Net::HTTP#read_timeout doesn''t work so if the > connection is interrupted the download never endsI expect read_timeout uses Threads (ruby''s timeout library does) - but I''d pursue Mario''s advice as well.> Maybe someone will fix multithreading in wxRuby? I don''t know the > internals of wxRuby so I don''t know how easy/difficult it is to fix > it. But I think that it would improve wxRuby greatly.Like I say, we could make a "best-guess" fix for common cases, but it''d be sub-optimal for many users. The other thing that is likely to change is the arrival of Ruby 1.9 in the next few months, which has operating system threads instead of green threads. cheers alex Alex