0

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?

2
  • Where does this exception thrown ? Commented Nov 30, 2011 at 9:26
  • the exception threw at SplitTextIntoLines(outputTextBox.Text, 1); Commented Dec 1, 2011 at 6:55

1 Answer 1

1

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);
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Make sure whenever you are accessing properties of UI control you do that on dispatcher thread. In your code you are accessing Text property of textboxes at multiple places. Is this code invoked from dispatcher thread?
Is exception still at SplitTextIntoLines(outputText, 1)?
so, this is the complete log: UpdateWP version 4.8.2345.0Error: Zune is currently running, please close Zune before using this tool.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.