-
I tried using ScriptFunctionInstance.Invoke to call Javascript callbacks from C# but get MemoryLimitExceededException or TimeoutException when called multiple times, although this, at least I think, should not be the case. Example which throws MemoryLimitExceededException : var engine = new Engine(options => { options.LimitMemory(1_000_000); });
engine.Execute("var callback = function(arg) {}");
var callback = engine.GetValue("callback") as ScriptFunctionInstance;
var testData = "Lorem Ipsum Dolor Sit Amet";
for (int i = 0; i < 100000; i++)
{
callback.Invoke(testData); // Throws MemoryLimitExceededException after 2258 iterations.
} Example which throws TimeoutException: var engine = new Engine(options => { options.TimeoutInterval(TimeSpan.FromSeconds(5)); });
engine.Execute("var callback = function(arg) {}");
var callback = engine.GetValue("callback") as ScriptFunctionInstance;
var testData = "Lorem Ipsum Dolor Sit Amet";
for (int i = 0; i < 10; i++)
{
callback.Invoke(testData); // Throws TimeoutException after 5 iterations
Thread.Sleep(1000);
} Is the ScriptFunctionInstance.Invoke method meant to be called externally, or would I be better off using Engine.Invoke instead? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Generally it's always better to route calls through Engine instance. Engine knows how to reset state between calls. You could even go one step further and set the |
Beta Was this translation helpful? Give feedback.
Generally it's always better to route calls through Engine instance. Engine knows how to reset state between calls.
You could even go one step further and set the
testData
as variable into engine and then callengine.Evaluate("callback(testData)")
, that's something I find a natural way to interact.