0

I have several Jint engines running different scripts. Sometimes I need to pause their execution and resume it later. My solution at the moment is to run each engine in its own thread and then use ManualResetEvent or similar to pause them as needed. This works fine.

Unfortunately in production, I cannot afford giving a thread to each Jint engine. The ideal scenario is if I could just run them all from the main thread, and pause each as needed e.g.

engines[6338].Pause();
...
engines[6338].Resume();

Without blocking the main thread either (due to UI).

I checked the Jint Debugger example as it probably has functionality to pause engines, but their solution/suggestion is actually the same I had originally: to use threads if you don't want to block the main thread:

private void Pause(DebugInformation e)
{
    currentInfo = e;
    string line = sources.GetLine(e.Location);

    // Output the location we're at:
    commandLine.OutputPosition(e.Location, line);

    // In this - single threaded - example debugger, we let Console.ReadLine take care of blocking the execution.
    // In debuggers involving a UI or in a debug server, we'd need script execution to be on a separate thread
    // from the UI/server, and use e.g. a ManualResetEvent, or a message queue loop here to block until the user
    // signals that the execution should continue.
    commandLine.Input();
}

Is there a way to pause/resume a Jint engine's execution without using threading?

Saturn
  • 17,888
  • 49
  • 145
  • 271
  • 1
    Jint executes it's code synchronously so I don't even know how it's possible to use a ManualResetEvent to pause it anyway. You call Execute, Jint executes the JavaScript and then returns. There is not enough information in this question. – Niya Mar 11 '23 at 01:42
  • @Niya what other information do you need? I think the question's pretty clear. An MRE can stop the engine (by pausing the thread) mid-execution - you can try it out in a manner of ways e.g. have your javascript call some C# method mid-execution which in turn uses the MRE etc. – Saturn Mar 11 '23 at 20:07
  • Your question is clear but you also say you want your engines not to block the UI. However if you run them on the UI thread, that's what they will do, even if they do periodically invoke callbacks into your .Net application. Your requirement of not blocking the main thread requires that your engines run on other threads. So I'm now wondering how are they not going to block the UI if you run them on the main thread. I'm missing something here. – Niya Mar 12 '23 at 11:02
  • It would help if you could give like a small mock-up of exactly what your Engine is doing. It doesn't have to be your actual code but something similar that can illustrate the relationship between your JavaScript code and your .Net application. – Niya Mar 12 '23 at 11:14
  • Sounds like what you are trying to ask is not really about pausing jint (which would be near on impossible cause of the way it works), but actually want it to pause calling C# hosted services. You should probably use promises to call C# and block in those methods. or in your JS call a `await blah.pausedAsync()` to call C# and see if it should be paused. – Michael Coxon Mar 16 '23 at 10:28

0 Answers0