# Killing a thread

**URL:** <https://www.ruby-forum.com/t/killing-a-thread/179328>\
**Category:** Ruby\
**Created:** [November 22, 2009, 10:02am UTC](https://www.ruby-forum.com/t/killing-a-thread/179328 "2009-11-22T10:02:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [November 22, 2009, 10:02am UTC](https://www.ruby-forum.com/t/killing-a-thread/179328/1 "2009-11-22T10:02:07Z")

</div>

This code starts my application in a windows thread fine but the last  
line does not kill the application that got fired up initially. How  
come?

x = Thread.new do  
system(‘app.exe’)  
end  
sleep 10  
Thread.kill(x)

David

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [November 22, 2009, 10:34am UTC](https://www.ruby-forum.com/t/killing-a-thread/179328/2 "2009-11-22T10:34:38Z")

</div>

David S. wrote:

> system(‘app.exe’)

## This starts a subprocess which cannot be killed by killing the thread. Use Kernel#spawn (that should work on Windows, too, I think) instead and kill the subprocess:

## pid = spawn(“app.exe”) sleep 10 Process.kill(“KILL”, pid)

Note that you don’t need the thread anymore.

Marvin

PS: You may have a look here:  
[http://wiki.ruby-portal.de/Tricks#Externe\_Programme\_starten](http://wiki.ruby-portal.de/Tricks#Externe_Programme_starten)  
😉

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [November 23, 2009, 1:55am UTC](https://www.ruby-forum.com/t/killing-a-thread/179328/3 "2009-11-23T01:55:37Z")

</div>

Doesn’t work on Windows.

irb(main):004:0\> $pid = spawn(‘tftpd32.exe’)  
NoMethodError: undefined method `spawn’ for main:Object  
from (irb):4

Thought I needed popen4 but that gem didn’t make any difference.

Marvin GÃ¼lker wrote:

> David S. wrote:
> 
> > system(‘app.exe’)
> 
> ## This starts a subprocess which cannot be killed by killing the thread. Use Kernel#spawn (that should work on Windows, too, I think) instead and kill the subprocess:
> 
> ## pid = spawn(“app.exe”) sleep 10 Process.kill(“KILL”, pid)
> 
> Note that you don’t need the thread anymore.
> 
> Marvin
> 
> PS: You may have a look here:  
> [http://wiki.ruby-portal.de/Tricks#Externe\_Programme\_starten](http://wiki.ruby-portal.de/Tricks#Externe_Programme_starten)  
> 😉

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [November 23, 2009, 2:41pm UTC](https://www.ruby-forum.com/t/killing-a-thread/179328/4 "2009-11-23T14:41:31Z")

</div>

David S. wrote:

> Doesn’t work on Windows.
> 
> irb(main):004:0\> $pid = spawn(‘tftpd32.exe’)  
> NoMethodError: undefined method `spawn’ for main:Object  
> from (irb):4
> 
> Thought I needed popen4 but that gem didn’t make any difference.
> 
> Marvin GÃ¼lker wrote:
> 
> > David S. wrote:
> > 
> > > system(‘app.exe’)
> > 
> > ## This starts a subprocess which cannot be killed by killing the thread. Use Kernel#spawn (that should work on Windows, too, I think) instead and kill the subprocess:
> > 
> > ## pid = spawn(“app.exe”) sleep 10 Process.kill(“KILL”, pid)
> > 
> > Note that you don’t need the thread anymore.
> > 
> > Marvin
> > 
> > PS: You may have a look here:  
> > [http://wiki.ruby-portal.de/Tricks#Externe\_Programme\_starten](http://wiki.ruby-portal.de/Tricks#Externe_Programme_starten)  
> > 😉

Oh, sorry. It’s some time ago since I last worked with processes on a  
Windows system. You then may have a look at the win32-process gem from  
the win32-utils project: [http://rubyforge.org/projects/win32utils/](http://rubyforge.org/projects/win32utils/)

Hope that helps,

Marvin
