# Killing a thread

**URL:** <https://rubytalk.org/t/killing-a-thread/56336>\
**Category:** ruby-talk\
**Created:** [22 November 2009 09:03 UTC](https://rubytalk.org/t/killing-a-thread/56336 "2009-11-22T09:03:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Schulberg](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@David\_Schulberg](https://rubytalk.org/u/David_Schulberg)\
**Post date:** [22 November 2009 09:03 UTC](https://rubytalk.org/t/killing-a-thread/56336/1 "2009-11-22T09:03:11Z")

</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  
&nbsp;&nbsp;&nbsp;&nbsp;system('app.exe')  
end  
sleep 10  
Thread.kill(x)

David

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Marvin\_GA\_lker](https://avatars.discourse-cdn.com/v4/letter/m/e95f7d/32.png) [@Marvin\_GA\_lker](https://rubytalk.org/u/Marvin_GA_lker)\
**Post date:** [22 November 2009 09:34 UTC](https://rubytalk.org/t/killing-a-thread/56336/2 "2009-11-22T09:34:46Z")

</div>

David Schulberg 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)  
> 😉  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![David\_Schulberg](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@David\_Schulberg](https://rubytalk.org/u/David_Schulberg)\
**Post date:** [23 November 2009 00:55 UTC](https://rubytalk.org/t/killing-a-thread/56336/3 "2009-11-23T00:55:39Z")

</div>

Doesn't work on Windows.

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

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

Marvin Gülker wrote:

> **···**
>
> > David Schulberg 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)  
> > 😉
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Marvin\_GA\_lker](https://avatars.discourse-cdn.com/v4/letter/m/e95f7d/32.png) [@Marvin\_GA\_lker](https://rubytalk.org/u/Marvin_GA_lker)\
**Post date:** [23 November 2009 13:41 UTC](https://rubytalk.org/t/killing-a-thread/56336/4 "2009-11-23T13:41:33Z")

</div>

David Schulberg wrote:

> Doesn't work on Windows.
> 
> irb(main):004:0\> $pid = spawn('tftpd32.exe')  
> NoMethodError: undefined method `spawn' for main:Object  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):4
> 
> Thought I needed popen4 but that gem didn't make any difference.
> 
> Marvin Gülker wrote:
> 
> > David Schulberg 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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
