命令行应用程序的异步输出
本文关键字:输出 异步 应用程序 命令行 | 更新日期: 2023-09-27 18:16:06
我做了一个更新设备的应用程序,原来是使用命令提示符,所以,我把它放在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();
}
,我添加了SplitTextIntoLines()到事件处理程序
但是,它导致InvalidOperationException:调用线程不能访问这个对象,因为它是另一个线程拥有的。
所以,。我应该怎么做才能使这个应用程序的输出整齐?
使用如下:
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);
}