2011-08-12 67 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

如果你想在同一时间运行多个程序,那么你应该看看任务库。您可以使任务按并行或串行方式运行,具体取决于系统资源。您还可以跟踪完成情况,以便显示已完成工作的百分比。