C# 来自命令行应用程序的异步输出?

C# 来自命令行应用程序的异步输出?,c#,wpf,cmd,C#,Wpf,Cmd,我制作了一个用于更新设备的应用程序,它最初使用命令提示符,所以,我在WPF UI中制作了它 我为流程启动和接收数据事件处理程序以及捕获错误编写了这段代码 System.Diagnostics.Process StarterBackup_X64 = new System.Diagnostics.Process(); StarterBackup_X64.StartInfo.FileName = X64;

我制作了一个用于更新设备的应用程序,它最初使用命令提示符,所以,我在WPF UI中制作了它

我为流程启动和接收数据事件处理程序以及捕获错误编写了这段代码

System.Diagnostics.Process StarterBackup_X64 = new System.Diagnostics.Process();
                                        StarterBackup_X64.StartInfo.FileName = X64;
                                        var Starter = StarterBackup_X64;
                                        Starter.StartInfo.Arguments = string.Concat("/iu \"", textBox1.Text, " " + textBox2.Text, " " + textBox3.Text, " " + textBox4.Text, " " + textBox5.Text, " " + textBox6.Text, " " + textBox7.Text, " " + textBox8.Text, " " + textBox9.Text, " " + textBox10.Text, " " + textBox11.Text, " " + textBox12.Text, " " + textBox13.Text, " " + textBox14.Text, " " + textBox15.Text, " " + textBox16.Text, " " + textBox17.Text, " " + textBox18.Text, " " + textBox19.Text, " " + textBox20.Text, "\" /enablebackup");
                                        //Starter.StartInfo.Arguments = string.Concat("/iu\"", textBox1.Text + textBox2.Text + textBox3.Text + textBox4.Text + textBox5.Text + textBox6.Text + textBox7.Text + textBox8.Text + textBox9.Text + textBox10.Text + textBox11.Text + textBox12.Text + textBox13.Text + textBox14.Text + textBox15.Text + textBox16.Text + textBox17.Text + textBox18.Text + textBox19.Text + textBox20.Text, "\" /enablebackup");
                                        StarterBackup_X64.StartInfo.CreateNoWindow = true;
                                        StarterBackup_X64.StartInfo.RedirectStandardOutput = true;
                                        StarterBackup_X64.StartInfo.UseShellExecute = false;
    StarterBackup_X64.OutputDataReceived += new       DataReceivedEventHandler(StarterBackup_X64_OutputDataReceived);
                                        StarterBackup_X64.Start();
    StarterBackup_X64.BeginOutputReadLine();
    installUpdateButton.Content = "Updating device.....";
    installUpdateButton.IsEnabled = false;
    restoreDeviceButton.IsEnabled = false;

    string UpdaterLog = outputTextBox.Text;

    if (UpdaterLog.ToString().IndexOf("no devices were found") > -1)
    {
              WPECore.ResTable.DeviceNotFound();
    }

    else if (UpdaterLog.ToString().IndexOf("error:") > -1)
    {
              WPECore.ResTable.UpdateWPCrashed();
    }

    else if (UpdaterLog.ToString().IndexOf("error message:") > -1)
    {
               WPECore.ResTable.UpdateWPCrashed();
    }

    void StarterBackup_X64_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        outputTextBox.Dispatcher.BeginInvoke(new Action(() => { outputTextBox.Text += e.Data;  }), null);
        SplitTextIntoLines(outputTextBox.Text, 1);


    }
而且,应用程序运行,但是,输出不是一个整洁的形式,所以,我尝试编写这个函数

        public static string[] SplitTextIntoLines(string title, int width)
    {
        System.Collections.Generic.List<System.String> list;
        System.Text.StringBuilder stringBuilder;
        string str;
        string[] arrstr1;
        int i;
        list = new System.Collections.Generic.List<System.String>();
        stringBuilder = new System.Text.StringBuilder();
        arrstr1 = title.Split(new char[] {
            ' '});
        i = 0;
        while (i < arrstr1.Length)
        {
            str = arrstr1[i];
            if (((stringBuilder.Length + str.Length) + 1) <= width)
            {
                stringBuilder.Append(' ');
                stringBuilder.Append(str);
            }
            else
            {
                list.Add(stringBuilder.ToString().Trim());
                stringBuilder = new System.Text.StringBuilder(str);
            }
            i++;
        }
        list.Add(stringBuilder.ToString().Trim());
        return list.ToArray();
    }
public静态字符串[]拆分文本为行(字符串标题,整数宽度)
{
System.Collections.Generic.List列表;
System.Text.StringBuilder StringBuilder;
字符串str;
字符串[]arrstr1;
int i;
list=新的System.Collections.Generic.list();
stringBuilder=new System.Text.stringBuilder();
arrstr1=标题.拆分(新字符[]{
' '});
i=0;
而(i
string outputText;
    void StarterBackup_X64_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
            outputTextBox.Dispatcher.Invoke(new Action(() => { outputTextBox.Text += e.Data; outputText = outputTextBox.Text; }), null);
            SplitTextIntoLines(outputText, 1);
    }

在何处引发此异常?在SplitTextIntoLines(outputTextBox.Text,1)引发的异常;确保每次访问UI控件的属性时,都在调度程序线程上执行此操作。在代码中,您在多个位置访问文本框的文本属性。此代码是否从调度程序线程调用?异常是否仍在SplitTextIntoLines(outputText,1)这是完整的日志:UpdateWP版本4.8.2345.0错误:Zune当前正在运行,请在使用此工具之前关闭Zune。