I am trying to make a Progress bar for some long running code in my ruby API. The best idea I could come up with so far is making this in an HTML Document and updating throughout my ruby code via WebDialog.execute_script(). Everything works in theory( I’ve tested line by line in the Ruby Console ) but when I actually run my code, the html window opens, but never actually loads until the code is finished runnning.
Can anyone help me solve this issue? or suggest a better/easier approach to a visual progress bar?
Thanks in advance!
any visual process will increase the time your code takes by the overhead needed to capture and display the progress…
it’s simpler to write to the prompt fro inside your tool…
it may have less overhead, I’m unsure…
The problem is that WebDialogs also run in the same thread as Ruby - the main SU thread. When Ruby is busy working it blocks messages to update the UI - including webdialogs. Using
Sketchup.set_status_text also suffer the same thing.
Some people have used timers to slice up their work - allowing window messages to pump between timer iterations. However it has big effect on performance.
I believe I remember seeing on another post that even though multi threading is possible in ruby, it is not in Sketchup. Is this true?
This is a Ruby limitation that SketchUp can’t work around. Ruby 2.0 uses native OS threads to implement Ruby Threads, but due to issues with libraries that aren’t thread safe it still manages the running of those threads from within the Ruby interpreter process. In a standalone Ruby script that isn’t a problem, but within SketchUp it means that the embedded Ruby interpreter blocks the rest of SketchUp while it is running.
In Ruby 1.8 the threads in Ruby where “Green Threads” - it was basically Ruby itself slicing the tasks.
In Ruby 2.0 the Ruby threads are native - but for some reason the main Ruby thread perform some handling of the child threads. If the main thread isn’t active then the child threads doesn’t process. If you start a Ruby thread you’d notice that they only run if you move the cursor around. It appear to be some clashes with Ruby being embedded into a GUI desktop application and how UI threads are run.
There is a way around it, by having a timer keeping the main thread alive. Though it’s kludgy.
Then there is the issue of the SketchUp API only working on the main thread. You cannot interact with it from other threads. That means that threading would only be useful if you do some longer calculations that doesn’t need to interact with the SketchUp API in any way. I’ve done something like that for when I need to do some background HTTP requests - but you’re out of luck of you try to create a progressbar when building SU geometry.
I have experimented with some Win32 API calls under windows - displaying progressbar in the taskbar. Supported by Windows7 and newer. However, there is no OSX alternative for that.
Could you share a snippet for this?
It’s on my list of things to upload to GitHub. It’s a Ruby C Extension. Need to detangle it and clean up.
i make a progress like this,
，and if you operate other function of sketchup during this period, the sketchup will crash.
Did you submit the BugSpats? (What version of SketchUp?)
OH:sweat_smile:，my English not is good. sketchup only like sleep and wait ruby code. And this “puts progress” can not display in this ruby console Until the long time ruby code is complete. my sketchup pro is 2018(simple chinese china).