2011-08-12 145 views
0

我目前正在編寫一個輕量級程序,它將許多命令行和其他外部進程整合到一個應用程序中。外部進程的進度條

目前,我面臨着使用系統信息過程提取系統信息的挑戰。

我已經成功編寫了按鈕來調用系統信息進程,並將輸出重定向到文本字段。

我現在試圖在我的WPF窗口底部有一個進度條,因爲它需要一些時間來加載系統信息。

由於我不知道從外部過程中獲得準確持續時間的方法,因此我試圖使用「選取框」樣式。

我一直在下面的例子在這裏stackoverflow(Windows Forms ProgressBar: Easiest way to start/stop marquee?),以及其他網站,但還沒有能夠確定在哪裏把代碼,以便進度條滾動while systeminfo運行時,並停止完成。

我目前的代碼(沒有progressbar1.Style = ProgressBarStyle.Marquee;)在下面。

任何有關在何處放置代碼或使用何種語法的建議都將不勝感激。先謝謝你!

private void btnSystemInfo_Click(object sender, RoutedEventArgs e) 
     { 


      // Get system info 
      Process info = new Process(); 
      info.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
      info.StartInfo.FileName = "C:\\Windows\\system32\\systeminfo.exe"; 
      info.StartInfo.Arguments = "-S " + context; 
      info.StartInfo.UseShellExecute = false; 
      info.StartInfo.CreateNoWindow = true; 
      info.StartInfo.RedirectStandardOutput = true; 

      info.Start(); 

      string infoOutput = info.StandardOutput.ReadToEnd(); 
      info.WaitForExit(); 

      // Write to the txtInfo text box 
      txtInfo.Text = "System Info: " + infoOutput; 
      txtInfo.Foreground = Brushes.Black; 
      txtInfo.VerticalScrollBarVisibility = ScrollBarVisibility.Visible; 

      // Switch to the info tab 
      tabControl.SelectedIndex = 3; 
     } 

回答

4

你需要做的是移動的是聚集在BackgroundWorker線程和主UI線程啓動選取框系統信息的代碼。一旦你從BackgroundWorker線程的信號,它的工作已經完成,停止字幕和文本框中顯示的信息

void ButtonClickEvent() 
{ 
    BackgroundWorker bg = new BackgroundWorker(); 
    bg.DoWork += new DoWorkEventHandler(MethodToGetInfo); 
    bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted); 
    //show marquee here 
    bg.RunWorkerAsync(); 
} 

void MethodToGetInfo(Object sender, DoWorkEventArgs args) 
{ 
    // find system info here 
} 

void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs args) 
{ 
    //this method will be called once background worker has completed it's task 
    //hide the marquee 
    //update the textbox 

    //NOTE that this is not a UI thread so you will need BeginInvoke to execute something in the UI thread 
} 
+0

謝謝哈里斯哈桑!這是我一直在尋找的! – PsiLentRain

0

如果你想在同一時間運行多個程序,那麼你應該看看任務庫。您可以使任務按並行或串行方式運行,具體取決於系統資源。您還可以跟蹤完成情況,以便顯示已完成工作的百分比。