Search code examples
javascriptc#jurassic

In Jurassic, how do I limit script running time?


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);

Solution

  • 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 a ThreadAbortException in the thread that executes the script. One possibility is to run ScriptEngine.Execute() in a new thread and call thread.Abort() in the current thread if the new thread does not complete after a specific time.

    Source

    If you read through the documentation, they do have suggestions for how to do this but it is a bit lengthy and involved.