Reputation: 1061
I use IronPython script from C# like this:
scriptEngine = Pythn.CreateEngine();
var scriptSource = scriptEngine.CreateScriptSourceFromFile(pathToScriptFile, Encoding.Default, SourceCodeKind.File);
scriptSource.Execute(_mainScope);
Script that is being started lasts a while and I would like to know if it is possible to stop it somehow from C# perspective.
Upvotes: 0
Views: 600
Reputation: 346
Much like actual threads, there is no safe way to terminate running code from an external perspective, unless the running code provides a cancellation mechanism (e.g. like CancellationToken
in .NET) or if it is running in an external process, in which case you can safely kill it off.
You could also spawn a new thread, run the code on it and kill the thread, but killing threads is very dangerous, so I would strongly advise against doing so.
Upvotes: 1