tags:

views:

238

answers:

2

On clicking a slot , I change the contents of the slot to provide user feed back, then call some code which takes several seconds to run. However the changes to the slot do not render until after the 'slow' process has completed. Is there any way I can force the rendering to happen before the 'slow' code runs.

In the following example the user never sees 'Processing, please wait ...'

class MyTest < Shoes

  url '/', :index
  url '/result', :result

  def index
    stack do
      my_button=flow do
        image './assets/trees.jpg'
        para 'Process Image'
      end
      my_button.click do
        my_button.contents[0].hide
        my_button.contents[1].text="Processing, please wait ..."
        sleep(4) # Simulate slow process

        visit '/result'
      end
    end
  end
  def result
    stack do
      para "Finished processing"
    end
  end
end

Shoes.app

Looking through Shoes source code in ruby.c or canvas.c there are references to a repaint or paint canvas. Are they callable from within shoes?

Thanks in advance

+1  A: 

It's kinda hackish, but you can move the actual logic into a separate function like this:

def doStuff()
  sleep(4) # Simulate slow process

  visit '/result'
end

And use timer to run it in a separate thread:

my_button.click do 
  my_button.contents[0].hide
  my_button.contents[1].text="Processing, please wait ..."

  timer(0) { doStuff() }
end
Pesto
A: 

Hi Pesto,

Thank you. That works a treat. I've also worked out a solution using Threads :

      def doStuff
         sleep(4) # Simulate slow process
         visit '/result'
      end

      .
      .
      .

      my_button=stack do 
         image  "button_image.jpg"
          para  "Press me"
      end
      my_button.click do
          Thread.new do
             doStuff
          end
           my_button.contents[0].hide
          my_button.contents[1].text = "Processing, please wait ..."
      end

Both solutions seem to work as expected.