nam*_*los 10 ironpython python-embedding
Jef*_*rdy 10
这基本上是对IronPython控制台如何处理Ctrl-C的改编.如果你想检查来源,它就在BasicConsole和CommandLine.Run.
首先,在一个单独的线程上启动IronPython引擎(如您所愿).当您运行用户代码时,将其包装在一个try ... catch(ThreadAbortException)块中:
var engine = Python.CreateEngine();
bool aborted = false;
try {
engine.Execute(/* whatever */);
} catch(ThreadAbortException tae) {
if(tae.ExceptionState is Microsoft.Scripting.KeyboardInterruptException) {
Thread.ResetAbort();
aborted = true;
} else { throw; }
}
if(aborted) {
// this is application-specific
}
Run Code Online (Sandbox Code Playgroud)
现在,您需要保持对IronPython线程的引用.在表单上创建一个按钮处理程序,然后调用Thread.Abort().
public void StopButton_OnClick(object sender, EventArgs e) {
pythonThread.Abort(new Microsoft.Scripting.KeyboardInterruptException(""));
}
Run Code Online (Sandbox Code Playgroud)
该KeyboardInterruptException参数允许Python线程捕获ThreadAbortException并将其作为一个处理KeyboardInterrupt.
| 归档时间: |
|
| 查看次数: |
1590 次 |
| 最近记录: |