I am implementing Jurassic script engine, how do I limit how long a script will take?
string ExcutingFunction = " for(var i=0;i<1000000;i++){ i%2; } ";
ScriptEngine ScrptingEngine = new ScriptEngine();
ScrptingEngine.Execute(ExcutingFunction);
Jurassic does not have an built-in way to limit the execution time of a script. Because Jurassic compiles JavaScript methods into IL code, there is no easy way to provide a timeout functionality without affecting performance.
However, it is possible to use
Thread.Abort()
to raise aThreadAbortException
in the thread that executes the script. One possibility is to runScriptEngine.Execute()
in a new thread and callthread.Abort()
in the current thread if the new thread does not complete after a specific time.
If you read through the documentation, they do have suggestions for how to do this but it is a bit lengthy and involved.