# Disable Button while Thread does some work

**URL:** <https://www.ruby-forum.com/t/disable-button-while-thread-does-some-work/85030>\
**Category:** Ruby-Gnome 2\
**Created:** [February 9, 2007, 12:09pm UTC](https://www.ruby-forum.com/t/disable-button-while-thread-does-some-work/85030 "2007-02-09T12:09:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhstudio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@dhstudio](https://www.ruby-forum.com/u/dhstudio)\
**Post date:** [February 9, 2007, 12:09pm UTC](https://www.ruby-forum.com/t/disable-button-while-thread-does-some-work/85030/1 "2007-02-09T12:09:22Z")

</div>

Hi,

I want a button to kick off processing, and for the button to be  
disabled  
while processing is going on. The code below does this. However,  
I can’t click the button twice in a row, I need to click elsewhere  
(within the  
larger app, or outside it) and then come back to the button  
for the click to work.

Why is this so? What am i missing? Or is there a better way to do  
this?

Ruby 1.8.4, gtk-win32-2-8-18-rc1, ruby-gtk2-0-14-1

Thanks,  
Martin

# Here’s the code:

require ‘gtk2’  
Gtk.init  
win = Gtk::Window.new

button = Gtk::Button.new( “Do it” )

win.add( button )

button.signal\_connect(“clicked”){  
if( button.sensitive? )

```
 button.sensitive=false

 Thread.new{
   puts "running process"
   sleep 2
   button.sensitive=true
 }

```

end  
}

win.show\_all

# Gtk.main

* * *

Using Tomcat but need to do more? Need to support web services,  
security?  
Get stuff done quickly with pre-integrated technology to make your job  
easier.  
Download IBM WebSphere Application Server v.1.0.1 based on Apache  
Geronimo  
[http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642](http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642)

---

<div class="post-metadata">

**Author:** ![dhstudio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@dhstudio](https://www.ruby-forum.com/u/dhstudio)\
**Post date:** [February 9, 2007, 12:24pm UTC](https://www.ruby-forum.com/t/disable-button-while-thread-does-some-work/85030/2 "2007-02-09T12:24:23Z")

</div>

Martin P. wrote:

> 

try the modified one i attached, you might get the idea from it.  
while the button is insensitive it wont accept your clicks.

balint

---

<div class="post-metadata">

**Author:** ![dhstudio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@dhstudio](https://www.ruby-forum.com/u/dhstudio)\
**Post date:** [February 9, 2007, 4:54pm UTC](https://www.ruby-forum.com/t/disable-button-while-thread-does-some-work/85030/3 "2007-02-09T16:54:53Z")

</div>

Dobai-Pataky Bálint wrote:

> > Why is this so? What am i missing? Or is there a better way to do this?
> 
> try the modified one i attached, you might get the idea from it.  
> while the button is insensitive it wont accept your clicks.

Thanks, but I didn’t explain myself clearly! That is exactly the  
behaviour I  
want!

However, once the processing is done, and the button becomes sensitive  
again,  
the button only responds to clicks once the app has lost focus. If I  
click on the button, leave the mouse where it is, wait for the  
processing to  
finish and the button to become sensitive, and I click again, that click  
and  
all following clicks are ignored until I move the focus to another  
window,  
and then come back to the app and try to click again, which works.

I guess it’s a problem with threads and what button.sensitive=true does,  
and when it does it, which I don’t understand (yet!).

Martin

* * *

Using Tomcat but need to do more? Need to support web services,  
security?  
Get stuff done quickly with pre-integrated technology to make your job  
easier.  
Download IBM WebSphere Application Server v.1.0.1 based on Apache  
Geronimo  
[http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642](http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642)

---

<div class="post-metadata">

**Author:** ![dhstudio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@dhstudio](https://www.ruby-forum.com/u/dhstudio)\
**Post date:** [February 9, 2007, 5:35pm UTC](https://www.ruby-forum.com/t/disable-button-while-thread-does-some-work/85030/4 "2007-02-09T17:35:00Z")

</div>

Martin P. wrote:

> However, once the processing is done, and the button becomes sensitive again,  
> the button only responds to clicks once the app has lost focus. If I  
> click on the button, leave the mouse where it is, wait for the processing to  
> finish and the button to become sensitive, and I click again, that click and  
> all following clicks are ignored until I move the focus to another window,  
> and then come back to the app and try to click again, which works.
> 
> I guess it’s a problem with threads and what button.sensitive=true does,  
> and when it does it, which I don’t understand (yet!).

the example script we shared works as expected here.  
dev-ruby/ruby-gtk2-0.16.0  
dev-lang/ruby-1.8.5\_p12  
Gentoo Base System release 1.12.9

---

<div class="post-metadata">

**Author:** ![dhstudio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@dhstudio](https://www.ruby-forum.com/u/dhstudio)\
**Post date:** [February 12, 2007, 9:45am UTC](https://www.ruby-forum.com/t/disable-button-while-thread-does-some-work/85030/5 "2007-02-12T09:45:32Z")

</div>

Dobai-Pataky Bálint wrote:

> the example script we shared works as expected here.  
> dev-ruby/ruby-gtk2-0.16.0  
> dev-lang/ruby-1.8.5\_p12  
> Gentoo Base System release 1.12.9

Ok, looks like I need to upgrade.  
Thanks for your help,  
Martin.

* * *

Using Tomcat but need to do more? Need to support web services,  
security?  
Get stuff done quickly with pre-integrated technology to make your job  
easier.  
Download IBM WebSphere Application Server v.1.0.1 based on Apache  
Geronimo  
[http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642](http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642)
