# How can I cancel a running script

**URL:** <https://www.ruby-forum.com/t/how-can-i-cancel-a-running-script/169651>\
**Category:** IronRuby\
**Created:** [June 6, 2009, 8:59am UTC](https://www.ruby-forum.com/t/how-can-i-cancel-a-running-script/169651 "2009-06-06T08:59:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![acampos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@acampos](https://www.ruby-forum.com/u/acampos)\
**Post date:** [June 6, 2009, 8:59am UTC](https://www.ruby-forum.com/t/how-can-i-cancel-a-running-script/169651/1 "2009-06-06T08:59:07Z")

</div>

Hi,

I have an IronRuby script running that I’d like to be able to cancel  
from outside the script. Is there any way to do that?

ScriptEngine scriptEngine = Ruby.CreateEngine();  
ScriptScope scriptScope = scriptEngine.CreateScope();  
string script =  
“print “1\n”\n” +  
“print “2\n”\n” +  
“sleep(10)\n” +  
“print “3\n”\n”;  
ThreadPool.QueueUserWorkItem(delegate { scriptEngine.Execute(script,  
scriptScope); });

In the above example how could I cancel the script to stop it getting to  
3 without having to resort to aborting the thread it is running on?

Regards,

Aaron

---

<div class="post-metadata">

**Author:** ![acampos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@acampos](https://www.ruby-forum.com/u/acampos)\
**Post date:** [June 6, 2009, 7:18pm UTC](https://www.ruby-forum.com/t/how-can-i-cancel-a-running-script/169651/2 "2009-06-06T19:18:27Z")

</div>

The script itself might check some (global) variable time to time and if  
it is “on” terminate itself. For example, use Kernel#select with a  
timeout instead of sleep.

Tomas
