2011-08-12 12 views
0

私は現在、多くのコマンドラインと他の外部プロセスを1つのアプリケーションに統合する軽量プログラムを作成しています。外部プロセス用プログレスバー

現在、システム情報プロセスを使用してシステム情報を取得するという課題に直面しています。

システム情報プロセスを呼び出すためのボタンが正常にコーディングされ、出力がテキストフィールドにリダイレクトされました。

私が今試みているのは、システム情報を読み込むのに時間がかかるため、WPFウィンドウの下部にプログレスバーを置くことです。

外部プロセスから正確な時間を取得する方法がわからないので、私はマーキースタイルを使用しようとしています。

ここではstackoverflow(Windows Forms ProgressBar: Easiest way to start/stop marquee?)などのサイトで例を紹介していますが、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

テキストボックスに情報を表示しますタスクライブラリ。システムリソースに応じて、タスクを並列またはシリアルで実行できます。完了した作業を追跡して、完了した作業の%を表示することもできます。

関連する問題