在写入文本框时运行IronPython Async

本文关键字:运行 IronPython Async 文本 | 更新日期: 2023-09-27 18:15:50

我想知道如何运行一个非常长时间运行的python脚本异步,这样它就不会阻塞我的整个应用程序,但我也希望它的流输出被写入一个文本框,而它的运行。我设法弄明白了第一部分,但不明白第二部分。目前,文本框只在脚本完成后更新。下面是我的代码的重要部分:

MainPage.xaml.cs:

...
BoxStream bs = null;
public MainPage()
    {
        InitializeComponent();
        bs = new BoxStream(OutBox);
        ...
    }
...
private bool slice()
    {
        try
        {
            var ipy = Python.CreateEngine();
            var iscp = ipy.CreateScope();
            iscp.SetVariable("rootP", Directory.GetCurrentDirectory());
            ipy.ExecuteFile("Pathsetter.py", iscp);
            var ipath = ipy.GetSysModule().GetVariable("path");
            ScriptEngine m_engine = Python.CreateEngine();
            ScriptScope m_scope = m_engine.CreateScope();
            m_engine.GetSysModule().SetVariable("path", ipath);
            m_engine.Runtime.IO.SetOutput(bs, Encoding.UTF8);
            m_engine.Runtime.IO.SetErrorOutput(bs, Encoding.UTF8);
            /*string dir = Directory.GetCurrentDirectory();
            string ndir = dir.Replace(@"'", @"''");*/
            m_engine.ExecuteFile(Directory.GetCurrentDirectory() + "''Skeinforge''skeinforge_application''skeinforge_utilities''skeinforge_craft.py", m_scope);
            string stl_path = Directory.GetCurrentDirectory() + "''test.stl";
            object func = m_scope.GetVariable("makeGcode");
            object result = m_engine.Operations.Invoke(func, stl_path);
            Debug.WriteLine(result); 
        }
        catch (Exception ex)
        {
            Debug.WriteLine("Py error: " + ex.Message);
            return false;
        }
        return true;
    }
...
private void Slice_Button_Click(object sender, RoutedEventArgs e)
    {
        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += new DoWorkEventHandler(
        delegate(object o, DoWorkEventArgs args)
        {
            BackgroundWorker b = o as BackgroundWorker;
            slice();
        });
    }
...

BoxStream.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
using System.Windows.Controls;
namespace SkeinforgeWP8
{
    class BoxStream : Stream
    {
        TextBox _OutBox = null;
        public BoxStream(TextBox tBox)
        {
            Debug.WriteLine("Writing stream to box");
            _OutBox = tBox;
            _OutBox.Text = "";
        }
        public override void WriteByte(byte value)
        {
            base.WriteByte(value);
        }
        public override void Write(byte[] buffer, int offset, int count)
        {
            _OutBox.Text += Encoding.UTF8.GetString(buffer, offset, count);
        }
        public override int Read(byte[] buffer, int offset, int count)
        {
            throw new NotImplementedException();
        }
        public override void SetLength(long value)
        {
            throw new NotImplementedException();
        }
        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new NotImplementedException();
        }
        public override void Flush()
        {
            return;
        }
        public override long Position
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        public override long Length
        {
            get { throw new NotImplementedException(); }
        }
        public override bool CanWrite
        {
            get { return true; }
        }
        public override bool CanSeek
        {
            get { return false; }
        }
        public override bool CanRead
        {
            get { return false; }
        }
    }
}

我想这可能是相当简单的,但经过大量的搜索,我能找到的是信息告诉我使用BackgroundWorker,正如我上面所做的,这不起作用,虽然因为我的文本框没有得到实时更新。

在写入文本框时运行IronPython Async

由于文本框更新将在后台线程中进入,它将失败。如果你将TextBox更新与Dispatcher调用一起包装,它将在下一个UI线程tick上运行代码,并且应该正确更新:

    public override void Write(byte[] buffer, int offset, int count)
    {
        Deployment.Current.Dispatcher.BeginInvoke(() => {
            _OutBox.Text += Encoding.UTF8.GetString(buffer, offset, count);
        });
    }