librelist archives

« back to archive

Problem with buttons.

Problem with buttons.

From:
Harold Waterkeyn
Date:
2011-03-27 @ 13:58
Hi,

I'm having some problem with shoes and buttons.

I'm using shoes to make a nice GUI for my program (which is a complex 
simulation)

Here's my code :
Shoes.app {
   @b = button "Launch simulation"
   @b.click {
     @b.hide
     para "Starting simulation"
     sleep(5) # Huge number crunching
     para "Simulation done"
     @b.show
   }
}

The problem is that when I click on the button, the app freeze for 5 
seconds and then instantly, the button is hided then showed and the two 
para are printed. Why everything is done after the 5 second sleep ?
Maybe I should use Threads to run my simulation, but I don't know much 
about it :/

I hope my question is precise enough.

Thanks a lot for your help.

Sorry for my bad english.

Re: [shoes] Problem with buttons.

From:
Cecil Coupe
Date:
2011-03-28 @ 04:08
Harold,

  Your example misbehaves just as you say on Ubuntu. It doesn't surprise
me that sleep(5) hangs the gui. It's probably correct and you really do
need to use threads.

  Since I don't understand Ruby threading all that well and we need a
useful example/test case for Shoes and threads,  I rewrote your example
until I got it to work for me.

  I'm assuming that you really want to call another program do to the
simulation with a system() call so in my example I call foo.sh which
does the waiting/simulation

#!/bin/sh
echo "Starting"
sleep 5
echo "Finished"

The script that worked for me is

Shoes.app {
   @b = button "Launch simulation" do
     @b.hide
     para "Starting simulation"
     th = Thread.new do
        er = system './foo.sh'     
        para "Simulation done: #{er}"
        @b.show
     end
   end 
}

It would be nice to know if this works in OSX and Windows or
could be modified for an example to distribute with Shoes.

On Sun, 2011-03-27 at 15:58 +0200, Harold Waterkeyn wrote:
> Hi,
> 
> I'm having some problem with shoes and buttons.
> 
> I'm using shoes to make a nice GUI for my program (which is a complex 
> simulation)
> 
> Here's my code :
> Shoes.app {
>    @b = button "Launch simulation"
>    @b.click {
>      @b.hide
>      para "Starting simulation"
>      sleep(5) # Huge number crunching
>      para "Simulation done"
>      @b.show
>    }
> }
> 
> The problem is that when I click on the button, the app freeze for 5 
> seconds and then instantly, the button is hided then showed and the two 
> para are printed. Why everything is done after the 5 second sleep ?
> Maybe I should use Threads to run my simulation, but I don't know much 
> about it :/
> 
> I hope my question is precise enough.
> 
> Thanks a lot for your help.
> 
> Sorry for my bad english.

Re: [shoes] Problem with buttons.

From:
i5m
Date:
2011-03-28 @ 10:02
Cecil,

just had a quick look on Windows. Using foo.bat:

echo "Starting"
sleep 5
echo "Finished"

The Shoes app hangs after the system call finishes. It's like Shoes is
waiting for it to return something and it never does. However, doing

er = system 'foo.bat'

from a normal Ruby irb session works fine. Another Shoes quirk?

-----------------------
i5m.co.uk
GPG Key: 0xA18A602B



On Mon, Mar 28, 2011 at 5:08 AM, Cecil Coupe <ccoupe@cableone.net> wrote:
> Harold,
>

>  I'm assuming that you really want to call another program do to the
> simulation with a system() call so in my example I call foo.sh which
> does the waiting/simulation
>
> #!/bin/sh
> echo "Starting"
> sleep 5
> echo "Finished"
>
> The script that worked for me is
>
> Shoes.app {
>   @b = button "Launch simulation" do
>     @b.hide
>     para "Starting simulation"
>     th = Thread.new do
>        er = system './foo.sh'
>        para "Simulation done: #{er}"
>        @b.show
>     end
>   end
> }
>
> It would be nice to know if this works in OSX and Windows or
> could be modified for an example to distribute with Shoes.
>

Re: [shoes] Problem with buttons.

From:
ashbb
Date:
2011-03-28 @ 13:08
Hi Harold, Cecil, i5m and folks,

As i5m mentioned, I confirmed `The Shoes app hangs after the system call
finishes` with Shoes 3 on my Windows 7.

How about this one? ;-)

Shoes.app {
  @b = button "Launch simulation" do
    @b.hide
    para "Starting simulation"
    th = Thread.new do
      @er = system 'foo.bat'
    end
    timer do
      th.join
      para "Simulation done: #{@er}"
      @b.show
    end
  end
}

# foo.bat
echo "Starting"
sleep 5
echo "Finished"

ashbb

Re: [shoes] Problem with buttons.

From:
ashbb
Date:
2011-03-28 @ 13:15
Hi again,

How about another solution without Thread.new()? ;-)

Shoes.app{
  @b = button "Launch simulation"
  @b.click{
    @b.hide
    para "Starting simulation"
    window do
      every 1 do |i|
        para i
        if i > 4
          owner.instance_variable_set :@finished, true
          close
        end
      end
    end
  }

  a = animate do
    if @finished
      para "Simulation done"
      @b.show
      a.stop
    end
  end
}

ashbb

Re: [shoes] Problem with buttons.

From:
Cecil Coupe
Date:
2011-03-28 @ 20:28
Which 'shell' is being used for the 'system' command in shoes and is it
different in irb. if its this one

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true

Sleep is not a command.

On Mon, 2011-03-28 at 11:02 +0100, i5m wrote:
> Cecil,
> 
> just had a quick look on Windows. Using foo.bat:
> 
> echo "Starting"
> sleep 5
> echo "Finished"
> 
> The Shoes app hangs after the system call finishes. It's like Shoes is
> waiting for it to return something and it never does. However, doing
> 
> er = system 'foo.bat'
> 
> from a normal Ruby irb session works fine. Another Shoes quirk?
> 
> -----------------------
> i5m.co.uk
> GPG Key: 0xA18A602B
> 
> 
> 
> On Mon, Mar 28, 2011 at 5:08 AM, Cecil Coupe <ccoupe@cableone.net> wrote:
> > Harold,
> >
> 
> >  I'm assuming that you really want to call another program do to the
> > simulation with a system() call so in my example I call foo.sh which
> > does the waiting/simulation
> >
> > #!/bin/sh
> > echo "Starting"
> > sleep 5
> > echo "Finished"
> >
> > The script that worked for me is
> >
> > Shoes.app {
> >   @b = button "Launch simulation" do
> >     @b.hide
> >     para "Starting simulation"
> >     th = Thread.new do
> >        er = system './foo.sh'
> >        para "Simulation done: #{er}"
> >        @b.show
> >     end
> >   end
> > }
> >
> > It would be nice to know if this works in OSX and Windows or
> > could be modified for an example to distribute with Shoes.
> >

Re: [shoes] Problem with buttons.

From:
Harold Waterkeyn
Date:
2011-03-28 @ 19:50
Hi again,

Thanks everyone for helping me !

The "sleep 5" was just an abstraction of the simulation, I don't really 
call another program.

Here's my code :

Shoes.app {
    @b = button "Launch simulation" do
      @b.hide
      para "Starting simulation"

      s = Simulation.new
      s.run # This is like a big loop that freeze the GUI ....

      para "Simulation done"
      @b.show
      window do
        para "Result of the simulation #{s.result}"
      end
    end
}

I actually have two problem.

The first one is the freeze of the GUI during the simulation.
The second one is that nothing (i.e. para, @b.hide) is done until the 
simulation finished.

I tried some of your solutions but I can't get it to work :/

Thanks again for your help !

Harold.

28/03/2011 06:08, Cecil Coupe wrote :
> Harold,
>
>    Your example misbehaves just as you say on Ubuntu. It doesn't surprise
> me that sleep(5) hangs the gui. It's probably correct and you really do
> need to use threads.
>
>    Since I don't understand Ruby threading all that well and we need a
> useful example/test case for Shoes and threads,  I rewrote your example
> until I got it to work for me.
>
>    I'm assuming that you really want to call another program do to the
> simulation with a system() call so in my example I call foo.sh which
> does the waiting/simulation
>
> #!/bin/sh
> echo "Starting"
> sleep 5
> echo "Finished"
>
> The script that worked for me is
>
> Shoes.app {
>     @b = button "Launch simulation" do
>       @b.hide
>       para "Starting simulation"
>       th = Thread.new do
>          er = system './foo.sh'
>          para "Simulation done: #{er}"
>          @b.show
>       end
>     end
> }
>
> It would be nice to know if this works in OSX and Windows or
> could be modified for an example to distribute with Shoes.
>
> On Sun, 2011-03-27 at 15:58 +0200, Harold Waterkeyn wrote:
>> Hi,
>>
>> I'm having some problem with shoes and buttons.
>>
>> I'm using shoes to make a nice GUI for my program (which is a complex
>> simulation)
>>
>> Here's my code :
>> Shoes.app {
>>     @b = button "Launch simulation"
>>     @b.click {
>>       @b.hide
>>       para "Starting simulation"
>>       sleep(5) # Huge number crunching
>>       para "Simulation done"
>>       @b.show
>>     }
>> }
>>
>> The problem is that when I click on the button, the app freeze for 5
>> seconds and then instantly, the button is hided then showed and the two
>> para are printed. Why everything is done after the 5 second sleep ?
>> Maybe I should use Threads to run my simulation, but I don't know much
>> about it :/
>>
>> I hope my question is precise enough.
>>
>> Thanks a lot for your help.
>>
>> Sorry for my bad english.
>

Re: [shoes] Problem with buttons.

From:
Cecil Coupe
Date:
2011-03-28 @ 20:14
Harold,

  this should get you closer.


Shoes.app {
    @b = button "Launch simulation" do
      @b.hide
      para "Starting simulation"
      th = Thread.new do
        s = Simulation.new
        s.run # This is like a big loop that freeze the GUI ....

        para "Simulation done"
        @b.show
        window do
          para "Result of the simulation #{s.result}"
        end
      end
    end
}

in your Simulation.run method you MAY need a Thread.pass so the
Shoes/GUI thread gets enough time to finish displaying the "starting"
para.