How to abort IronPython script excecution

I did try scriptRunner.reset but it does not help. I cannot force users to use cancelation token correctly in script like in threaded sort sample. What is correct way to cancel script execution which is in progress.
I add sample project for best testing experience.

Generally the proper way to cancel the script execution is to use CancellationTokens as shown in Threading quick start demo.

You could use Thread.Abort for that, but it is strongly discouraged to the point the method in marked as obsolete in .NET 5:
Breaking change: Thread.Abort is obsolete - .NET | Microsoft Docs
c# - What’s wrong with using Thread.Abort() - Stack Overflow
c# - dotnet core equivalent to Thread.Abort - Stack Overflow

So if you would like to run the script in-process, the only practical way of stopping the script execution is by adding cancellation token (or any boolean flag) checks into your scripts.

Can you say the the propose of function scriptRun.Reset then? Does It do it’s function?

I do believe, that it is very important to allow scripts to be aborted. It is all about providing scripting capabilities to you app. You can’t expect users who will script to enforce use cancellation token. They a simple users not soft devs with C# parallel development skills…

And users must have options to abort script. There might be case like written endless loop. An then what?

Disclaimer: I give big minus for C# Core lang is only one language which can’t kill thread. Python , C++, delphi allows it. :confused:

Other ideas: How about isolated scripts, separated appdomains, which could be unloaded, and freed all resources with them?

Also are always null in objecte reference sample scriptObject = scriptRun.Run() as IDisposable; (Both for C# and Python scripting). I would call dispose. Hopefully causing script to exit.

Unfortunately, you cannot safely abort threads in .NET.
Unloading an AppDomain will effectively attempt to abort it’s threads, so this doesn’t solve the problem (see AppDomain.Unload(AppDomain) Method (System) | Microsoft Docs).

scriptObject = scriptRun.Run() as IDisposable; (Both for C# and Python scripting). I would call dispose. Hopefully causing script to exit.

The Run method returns the value returned by the script. If a script does not return anything, Run returns null.

To clarify, I would like to add that in principle, you could use Thread.Abort to stop your scripts in .NET Framework, and that might work in simple cases. However, this approach it strongly discouraged and might cause instability of your application in scenarios involving cancellation of your scripts.

Thank you. Yes, we are using it.
Also, it would be nice to move to .NET6 in future.
There is isolation sample & remoting, which can be used to send & retrieve data or call functions between apps. It would be nice that this feature would be in your library out of the box & ready to use like object reference sample.
I really do not know the final solution for .NET 6. However, our product is WIN Desktop app, so I guess, I will end up in getting thread Handle and Calling windows API for thread abortion.

Hi Audrius,

Could you please provide example of the objects that you’d like to exchange between applications, so we can see what would be the best way to include such functionality to our core libraries or demo projects?
Right now we have such functionality in DebugRemoteScript, which shows how to create wrapper around sample interface IScriptAPI for inter-process communications, however I’m not sure it can be made more generic.

Regards,
Dmitry

It can be gigabytes of data (Expecting up to 8 or 12GB of lines& fabrication speed/acceleration). Calling remote function to AddLine might take more time than sending packed memory block of 8GB. Currently I am only making first steps in our app. It will take some time till I get to actual implementation. Anyways, I mentioned it in windows app, so we might just send object via memory mapped file. Or jus call thread abortation via windows api. And you need general solution/cross platform. Currently via on .net framework therefore Thread.Abort is the best solution.