Simulating thread.interrupt main

From IronPython Cookbook

Posted to the IronPython Mailing List by Dino Viehland.

The Python threading API is available in IronPython, but the thread.interrupt_main() function isn't available. In CPython this raises a KeyboardInterrupt exception in the main thread. You can easily simulate this in IronPython by keeping a reference to the main thread and then raising a KeyboardInterruptException yourself.

In IronPython 2.x:

import clr
clr.AddReference('Microsoft.Scripting')
from Microsoft.Scripting.Shell import KeyboardInterruptException
from System import Threading

main = Thread.CurrentThread

main.Abort(KeyboardInterruptException(""))

In IronPython 1.x it's:

import clr
clr.AddReference('IronPython')
from IronPython.Runtime.Exceptions import PythonKeyboardInterruptException
from System import Threading

main = Thread.CurrentThread

main.Abort(PythonKeyboardInterruptException(""))


Back to Contents.

TOOLBOX
LANGUAGES