2014-02-27 32 views
0

我想用htop的一個(固定控制檯設計)類似的界面來構建控制檯應用程序。以下是htop控制檯設計的鏈接:http://upload.wikimedia.org/wikipedia/commons/b/b1/Htop.png。我想問如何構建這樣的應用程序,因爲我只知道C#的Console.Write()方法。我正在編寫一個簡單的程序,通過Process.Start()啓動應用程序,然後我通過Process.WorkingSet64監控例如他們的RAM使用情況,並通過每行簡單的Console.WriteLine()輸出到控制檯。但是,我怎麼能設計出像htop這樣的C#控制檯應用程序,因此它有固定的設計,例如每1秒刷新一次。由固定設計,我的意思是我這將是固定在控制檯上的位置,我會打印出進程名,內存使用情況,應用程序的名稱等。以下是我的程序代碼:像htop的C#控制檯應用程序設計

class Program 
{ 
    static void Main(string[] args) 
    { 
     string[] myApps = { "notepad.exe", "calc.exe", "explorer.exe" }; 

     Thread w; 
     ParameterizedThreadStart ts = new ParameterizedThreadStart(StartMyApp); 
     foreach (var myApp in myApps) 
     { 
      w = new Thread(ts); 
      w.Start(myApp); 
      Thread.Sleep(1000); 
     } 
    } 

    public static void StartMyApp(object myAppPath) 
    { 
     ProcessStartInfo myInfoProcess = new ProcessStartInfo(); 
     myInfoProcess.FileName = myAppPath.ToString(); 
     myInfoProcess.WindowStyle = ProcessWindowStyle.Minimized; 
     Process myProcess = Process.Start(myInfoProcess); 

     do 
     { 
      if (!myProcess.HasExited) 
      { 
       myProcess.Refresh(); // Refresh the current process property values. 
       Console.WriteLine(myProcess.ProcessName+" RAM: " + (myProcess.WorkingSet64/1024/1024).ToString() + "\n"); 
       Thread.Sleep(1000); 
      } 
     } 
     while (!myProcess.WaitForExit(1000)); 
    } 
} 

編輯:感謝您指向Console.SetCursorPosition @Jim Mischel。我想在我的應用程序中使用它,但現在我有另一個問題。我怎麼能傳遞給我的StartMyApp方法,索引號從myApps陣列,所以我可以做這樣的事情:

Console.WriteLine((Array.IndexOf(myApps, myAppPath) + " " + myProcess.ProcessName+" RAM: "+ (myProcess.WorkingSet64/1024/1024).ToString() + "\n"); 

這是我StartMyApp方法內。我使用的任何方法最終都會得到The name 'myApps' does not exist in the current context。這對我來說非常重要,所以我可以稍後使用Console.SetCursorPosition來設計我的應用程序,但我需要該索引號。所以我的輸出將是,例如:

0 notepad RAM: 4 
1 calc RAM: 4 
2 explorer RAM: 12 

回答

2

您想調用Console.SetCursorPosition來設置下一次寫入發生的位置。鏈接的MSDN主題有一個基本示例,可幫助您開始。

您還會對BackgroundColorForegroundColor以及其他可能的屬性感興趣。有關詳細信息,請參閱Console class文檔。

+0

感謝您指向'Console.SetCursorPosition'。我有另一個問題(編輯):) –