Suppose I have code like this for testing.
public class SimpleScheduler { public Script Script { get; set; } private Thread _worker; public void Schedule() { this._worker = new Thread(this.Script.Execute); this._worker.Start(); } public void Sleep() {
SimpleScheduler simply takes a Script object and tries to execute it in a separate thread.
public class Script { public string ID { get; set; } private ScriptSource _scriptSource; private ScriptScope _scope; private CompiledCode _code; private string source = @"import clr clr.AddReference('Trampoline') from Trampoline import PythonCallBack def Start(): PythonCallBack.Sleep()"; public Script() { _scriptSource = IronPythonHelper.IronPythonEngine.CreateScriptSourceFromString(this.source); _scope = IronPythonHelper.IronPythonEngine.CreateScope(); _code = _scriptSource.Compile(); } public void Execute() { _code.Execute(_scope); dynamic start = _scope.GetVariable("Start"); start(); } }
Script class tries to call the Sleep function from the PythonCallBack class and wants to pause for a while.
public static class PythonCallBack { public static SimpleScheduler Scheduler; static PythonCallBack() { Scheduler = new SimpleScheduler(); } public static void Sleep() { Scheduler.Sleep(); } }
PythonCallBack only to call the SimpleScheduler hibernation method.
Question: What is the best way to pause a thread that is running Script? and how to resume this thread?
source share