HTML Progress Bar


#1

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!


#2

any visual process will increase the time your code takes by the overhead needed to capture and display the progress…

you can do it by passing back and forth using javascript, but it does slow down as the process runs…

it’s simpler to write to the prompt fro inside your tool…

 Sketchup.set_status_text("Re-facing #{e.parent.name}...")

it may have less overhead, I’m unsure…

john


#3

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.


#4

thanks thomthom,
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?


#5

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.


#6

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.


#7

Could you share a snippet for this?


#8

It’s on my list of things to upload to GitHub. It’s a Ruby C Extension. Need to detangle it and clean up.