在C#中异步运行IronPython脚本

时间:2022-09-02 08:55:24

In C# 4.0 and IronPython 2.6, is it possible to execute a python script in it's own thread? I would like to spawn off the script after passing in some event handler objects so that it can update the GUI as it runs.

在C#4.0和IronPython 2.6中,是否可以在自己的线程中执行python脚本?我希望在传入一些事件处理程序对象后生成脚本,以便它可以在运行时更新GUI。

2 个解决方案

#1


3  

I would use a Task:

我会使用一个任务:

ScriptEngine engine = ...;
// initialize your script and events

Task.Factory.StartNew(() => engine.Execute(...));

The IronPython script will then run on a separate thread. Make sure your event handlers use the appropriate synchronization mechanism when updating the GUI.

然后,IronPython脚本将在单独的线程上运行。更新GUI时,确保事件处理程序使用适当的同步机制。

#2


3  

You could use a background worker to run the script on a separate thread. Then use the ProgressChanged and RunWorkerCompleted event handlers to update the ui.

您可以使用后台工作程序在单独的线程上运行脚本。然后使用ProgressChanged和RunWorkerCompleted事件处理程序更新ui。

  BackgroundWorker worker;
  private void RunScriptBackground()
  {
     string path = "c:\\myscript.py";
     if (File.Exists(path))
     {            
        worker = new BackgroundWorker();
        worker.DoWork += new DoWorkEventHandler(bw_DoWork);
        worker.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
        worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
        worker.RunWorkerAsync();
     }
  }

  private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  {
     // handle completion here
  }

  private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
  {
     // handle progress updates here
  }

  private void bw_DoWork(object sender, DoWorkEventArgs e)
  {
     // following assumes you have setup IPy engine and scope already
     ScriptSource source = engine.CreateScriptSourceFromFile(path);
     var result = source.Execute(scope);
  }

#1


3  

I would use a Task:

我会使用一个任务:

ScriptEngine engine = ...;
// initialize your script and events

Task.Factory.StartNew(() => engine.Execute(...));

The IronPython script will then run on a separate thread. Make sure your event handlers use the appropriate synchronization mechanism when updating the GUI.

然后,IronPython脚本将在单独的线程上运行。更新GUI时,确保事件处理程序使用适当的同步机制。

#2


3  

You could use a background worker to run the script on a separate thread. Then use the ProgressChanged and RunWorkerCompleted event handlers to update the ui.

您可以使用后台工作程序在单独的线程上运行脚本。然后使用ProgressChanged和RunWorkerCompleted事件处理程序更新ui。

  BackgroundWorker worker;
  private void RunScriptBackground()
  {
     string path = "c:\\myscript.py";
     if (File.Exists(path))
     {            
        worker = new BackgroundWorker();
        worker.DoWork += new DoWorkEventHandler(bw_DoWork);
        worker.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
        worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
        worker.RunWorkerAsync();
     }
  }

  private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  {
     // handle completion here
  }

  private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
  {
     // handle progress updates here
  }

  private void bw_DoWork(object sender, DoWorkEventArgs e)
  {
     // following assumes you have setup IPy engine and scope already
     ScriptSource source = engine.CreateScriptSourceFromFile(path);
     var result = source.Execute(scope);
  }