Reputation: 63
i made an app for updating device which originally using command prompt, so, i made it in WPF UI.
i wrote this code for process launching and data received event handler, and catching errors
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);
}
and, the app runs, but, the output is not in a neat form, so, i tried to write this function
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();
}
and, i added the SplitTextIntoLines() to event handler
but, it resulted on an InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
so,. what should i do to make this app's output is neat?
Upvotes: 0
Views: 459
Reputation: 939
Use following:
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);
}
Upvotes: 1