English 中文(简体)
如何在单独的线程中执行IronPython脚本,然后将其挂起?
原标题:How to execute IronPython script in separate thread and then suspend it?

让我们假设我有这样的代码进行测试。

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?

问题回答

Thread类有一个Suspend()Resume()[/code>方法。这些方法挂起并恢复线程。尽管有关于此方法的警告,但这应该不是问题,因为您正在一个已知的位置挂起。

另一种选择是使用事件。您可以为ScriptScriptScope类提供AutoResetEvent。然后,在Sleep()中,对事件调用WaitOne()。然后,从外部,当您希望线程恢复时,您可以调用Set()





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...