1

我想知道如何异步运行一个运行时间很长的 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 的信息,但这不起作用,因为我的文本框没有得到实时更新。

4

1 回答 1

0

由于 TextBox 更新将在后台线程中进行,因此它将失败。如果您通过调用 Dispatcher 来包装您的 TextBox 更新,它将在下一个 UI 线程滴答声中运行代码并且应该正确更新:

    public override void Write(byte[] buffer, int offset, int count)
    {
        Deployment.Current.Dispatcher.BeginInvoke(() => {
            _OutBox.Text += Encoding.UTF8.GetString(buffer, offset, count);
        });
    }
于 2013-05-28T23:36:27.357 回答