Pregunta

I want to build console application with similar interface like htop's one (fixed console design). Here is a link to htop console design: http://upload.wikimedia.org/wikipedia/commons/b/b1/Htop.png. I wanted to ask how can I build application like this as I only know C#'s Console.Write() method. I am writing simple program that is starting up applications via Process.Start() and then I am monitoring for example their RAM usage via Process.WorkingSet64 and outputing it via simple Console.WriteLine() each line to console. But how could I design C# console application like htop so it has fixed design that will be for example refreshing every 1 second. By fixed designed I mean that I it will be fixed position on the console where I will print out process names, ram usage, application name, etc. Here is my code of the program:

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

EDIT: Thanks for pointing to Console.SetCursorPosition @Jim Mischel. I want to use that in my application but now I have another problem. How could I pass to my StartMyApp method, the index number from myApps array so I could do something like:

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

That is inside my StartMyApp method. Any method I use I end up getting The name 'myApps' does not exist in the current context. This is very important for me so I could design my application later using Console.SetCursorPosition but I need that index number. So my output would be for example:

0 notepad RAM: 4
1 calc RAM: 4
2 explorer RAM: 12
¿Fue útil?

Solución

You want to call Console.SetCursorPosition to set the position where the next write will occur. The linked MSDN topic has a basic example that will get you started.

You'll also be interested in the BackgroundColor, ForegroundColor, and possibly other properties. See the Console class documentation for details.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top