# Best way to kill a thread started from a TkButton push

**URL:** <https://www.ruby-forum.com/t/best-way-to-kill-a-thread-started-from-a-tkbutton-push/98913>\
**Category:** Ruby\
**Created:** [July 18, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/best-way-to-kill-a-thread-started-from-a-tkbutton-push/98913 "2006-07-18T23:52:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtoy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f475e1/32.png) [@jtoy](https://www.ruby-forum.com/u/jtoy)\
**Post date:** [July 18, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/best-way-to-kill-a-thread-started-from-a-tkbutton-push/98913/1 "2006-07-18T23:52:34Z")

</div>

Hello all,

Looking for the best way to kill a thread started from  
a TkButton push.

I’m just using one thread that I would like to control  
via a Tk Start button and a Tk Stop button.

The reason for the question is that I’m using a method  
called stop\_thread that takes the thread to stop as  
it’s argument and simply using thread.kill, and  
setting my button states back to normal ; This for the  
most part works except when I call it from code as  
opposed to from a user event. The button states do  
not go back to normal for some reason when I call  
stop\_thread from code.

BTW it’s the tk8.3 lib on win32.

Thanks in advance,

Jason

---

<div class="post-metadata">

**Author:** ![jtoy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f475e1/32.png) [@jtoy](https://www.ruby-forum.com/u/jtoy)\
**Post date:** [July 19, 2006, 9:36pm UTC](https://www.ruby-forum.com/t/best-way-to-kill-a-thread-started-from-a-tkbutton-push/98913/2 "2006-07-19T21:36:13Z")

</div>

Jason T. wrote:

> it’s argument and simply using thread.kill, and  
> setting my button states back to normal ; This for the  
> most part works except when I call it from code as  
> opposed to from a user event. The button states do  
> not go back to normal for some reason when I call  
> stop\_thread from code.
> 
> BTW it’s the tk8.3 lib on win32.

I can’t help with your problem exactly, but this sounds like it is to do  
with the Binding of the variable(s).  
See the Pickaxe book: [http://www.rubycentral.com/book/ext\_tk.html](http://www.rubycentral.com/book/ext_tk.html)

An excerpt:  
“One small caution when using the code block form: the scope of  
variables is not what you think it is. The block is actually evaluated  
in the context of the widget’s object, not the caller’s. …”

Regards,  
JJ

---

<div class="post-metadata">

**Author:** ![jtoy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f475e1/32.png) [@jtoy](https://www.ruby-forum.com/u/jtoy)\
**Post date:** [July 20, 2006, 8:19pm UTC](https://www.ruby-forum.com/t/best-way-to-kill-a-thread-started-from-a-tkbutton-push/98913/3 "2006-07-20T20:19:04Z")

</div>

John J. wrote:

> Jason T. wrote:
> 
> > it’s argument and simply using thread.kill, and  
> > setting my button states back to normal ; This for the  
> > most part works except when I call it from code as  
> > opposed to from a user event. The button states do  
> > not go back to normal for some reason when I call  
> > stop\_thread from code.
> > 
> > BTW it’s the tk8.3 lib on win32.
> 
> I can’t help with your problem exactly, but this sounds like it is to do  
> with the Binding of the variable(s).  
> See the Pickaxe book: [http://www.rubycentral.com/book/ext\_tk.html](http://www.rubycentral.com/book/ext_tk.html)
> 
> An excerpt:  
> “One small caution when using the code block form: the scope of  
> variables is not what you think it is. The block is actually evaluated  
> in the context of the widget’s object, not the caller’s. …”
> 
> Regards,  
> JJ

Thanks, you got me thinking about the scope and it ended up being just  
that, the scope awareness caution excerpt you provided from the pickaxe  
book.

Regards,

Jason
