Created
March 8, 2019 20:39
-
-
Save Eyad-Bereh/c2b2bffaff718144ed697ec8da32f788 to your computer and use it in GitHub Desktop.
Simple task manager in C# , built using .NET Core with .NET SDK 2.2.104
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Collections; | |
namespace Test | |
{ | |
public class Program | |
{ | |
public static int Main(string[] args) | |
{ | |
Hashtable arguments = new Hashtable(); // A hashtable to store key/value command line arguments | |
bool show_priority = false; // Does the caller want to see priorities ? | |
bool show_threads = false; // Does the caller want to see number of threads for each process ? | |
// At first , we'll be filtering out the non key/value arguments | |
for (int i = 0; i < args.Length; i++) { | |
if (args[i].Contains('=')) { | |
continue; | |
} | |
if (args[i] == "--show-priority") { | |
show_priority = true; | |
} | |
if (args[i] == "--show-threads") { | |
show_threads = true; | |
} | |
} | |
// Now , we'll be filtering out the key/value arguments | |
for (int i = 0; i < args.Length; i++) { | |
if (!args[i].Contains('=')) { | |
continue; | |
} | |
string key = args[i].Split('=')[0]; | |
string value = args[i].Split('=')[1]; | |
arguments.Add(key, value); | |
} | |
int refresh_rate = 5000; // A refresh rate that indicates the required period before updating processes list | |
int refresh_count = 1; // Defines how many times the processes list should be updated | |
if (arguments.ContainsKey("--refresh-rate")) { | |
refresh_rate = Convert.ToInt32(arguments["--refresh-rate"]); | |
if (refresh_rate < 0) { | |
Console.WriteLine("Notice: Refresh Rate is negative , setting it now to 5000 ms ..."); | |
refresh_rate = 5000; | |
} | |
} | |
if (arguments.ContainsKey("--refresh-count")) { | |
refresh_count = Convert.ToInt32(arguments["--refresh-count"]); | |
if (refresh_count <= 0) { | |
Console.WriteLine("Notice: Refresh Rate is negative or zero , setting it now to 1 ..."); | |
refresh_count = 1; | |
} | |
} | |
for (int i = 0; i < refresh_count; i++) { | |
System.Diagnostics.Process.Start("clear"); // Clear console screen | |
Process[] Processes = System.Diagnostics.Process.GetProcesses(); // Get processes list | |
Console.Write("{0,10}\t|{1,20}", "ID", "Name"); | |
if (show_priority) { | |
Console.Write("\t|{0,15}", "Priority"); | |
} | |
if (show_threads) { | |
Console.Write("\t|{0,10}", "Threads"); | |
} | |
Console.WriteLine(); | |
Console.WriteLine("\t-------------------------------------------------------------------------"); | |
foreach (Process P in Processes) { | |
Console.Write("{0,10}\t|{1,20}", P.Id, P.ProcessName); | |
if (show_priority) { | |
Console.Write("\t|{0,15}", P.PriorityClass); | |
} | |
if (show_threads) { | |
Console.Write("\t|{0,10}", P.Threads.Count); | |
} | |
Console.WriteLine(); | |
} | |
System.Threading.Thread.Sleep(refresh_rate); // Hold on for a while | |
} | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment