让我们假设我有这样的代码进行测试。
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
只接受Script
对象,并尝试在单独的线程中执行它。
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();
}
}
<code>Script</code>类试图回调<code>PythonCallBack</code<类的Sleep函数,并希望暂停一段时间。
public static class PythonCallBack
{
public static SimpleScheduler Scheduler;
static PythonCallBack()
{
Scheduler = new SimpleScheduler();
}
public static void Sleep()
{
Scheduler.Sleep();
}
}
PythonCallBack只用于调用SimpleScheduler的sleep方法。
Question: What is the best way to suspend thread, which executes Script? and then how to resume this thread execution?