2011-11-30 49 views
0

我做了一個應用程序更新設備,最初使用命令提示符,所以,我在WPF用戶界面。從命令行應用程序異步輸出?

我寫了這個代碼程序啓動和數據接收的事件處理程序,並捕捉錯誤

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:調用線程不能訪問這個對象是因爲不同的線程擁有它。

等等。我該怎麼做才能使這個應用程序的輸出是整潔?

+0

哪裏此異常拋出? – Tigran

+0

在SplitTextIntoLines(outputTextBox.Text,1)處拋出異常; – user1072976

回答

1

使用下列內容:

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); 
    } 
+0

仍然是一樣的:'( – user1072976

+0

)確保在訪問UI控件的屬性時,你在調度程序線程上做到這一點,在你的代碼中,你在多個地方訪問文本框的Text屬性,這個代碼是從調度程序線程調用的嗎? – Amit

+0

仍然在SplitTextIntoLines(outputText,1)? – Amit

相關問題