Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 822709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:50:16+00:00 2026-05-15T02:50:16+00:00

BACKGROUND I’ve successfully embedded IronPython in my WinForm apps using techniques like the one

  • 0

BACKGROUND

  • I’ve successfully embedded IronPython in my WinForm apps using techniques like the one described here: http://blog.peterlesliemorris.com/archive/2010/05/19/embedding-ironpython-into-a-c-application.aspx
  • In the context of the embedding, my user may any write loops, etc.
  • I’m using the IronPython 2.6 (the IronPython for .NET 2.0 and IronPython for .NET 4.0)

MY PROBLEM

  • The users will need to interrupt the execution of their code
  • In other words they need something like the ability to hit CTRL-C to halt execution when running Python or IronPython from the cmdline
  • I want to add a button to the winform that when pressed halts the execution, but I’m not sure how to do this.

MY QUESTION

  • How can I make it to that pressing the a “stop” button will actually halt the execution of the using entered IronPython code?

NOTES

  • Note: I don’t wish to simply throw away that “session” – I still want the user to be able to interact with session and access any results that were available before it was halted.
  • I am assuming I will need to execute this in a separate thread, any guidance or sample code in doing this correctly will be appreciated.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:50:17+00:00Added an answer on May 15, 2026 at 2:50 am

    This is basically an adaptation of how the IronPython console handles Ctrl-C. If you want to check the source, it’s in BasicConsole and CommandLine.Run.

    First, start up the IronPython engine on a separate thread (as you assumed). When you go to run the user’s code, wrap it in a try ... catch(ThreadAbortException) block:

    var engine = Python.CreateEngine();
    bool aborted = false;
    try {
        engine.Execute(/* whatever */);
    } catch(ThreadAbortException tae) {
        if(tae.ExceptionState is Microsoft.Scripting.KeyboardInterruptException) {
            Thread.ResetAbort();
            aborted = true;
        } else { throw; }
    }
    
    if(aborted) {
        // this is application-specific
    }
    

    Now, you’ll need to keep a reference to the IronPython thread handy. Create a button handler on your form, and call Thread.Abort().

    public void StopButton_OnClick(object sender, EventArgs e) {
        pythonThread.Abort(new Microsoft.Scripting.KeyboardInterruptException(""));
    }
    

    The KeyboardInterruptException argument allows the Python thread to trap the ThreadAbortException and handle it as a KeyboardInterrupt.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 418k
  • Answers 418k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Let me first say that I find it a bit… May 15, 2026 at 9:59 am
  • Editorial Team
    Editorial Team added an answer I don't think there is anything inherently wrong in calling… May 15, 2026 at 9:59 am
  • Editorial Team
    Editorial Team added an answer Create a third interface I3 extends I1 and I2. Then… May 15, 2026 at 9:59 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.