Domain – container for processes. Domain include processes. It is .NET technology – to make work more safety, also for multiPlatforms, more efficient with resources consumption.
Process – running program, container for threads. It has PID.
Thread – way of execution. There is always main thread. Possible many threads. If something resoursable – put in additional thread, main thread will work correctly. Process includes threads. Principle of thread is to give quantum of time to each thread in cycle and remember its state to next quantum of time – it is like a wheel in casino.
Thread has local storage and call stack!
Processes and .NET
System.Diagnostics has number of classes for interaction.
System.Diagnostics.Process
System.Diagnostics.ProcessModule
System.Diagnostics.ProcessModuleCollection
System.Diagnostics.ProcessStartInfo
System.Diagnostics.ProcessThread
System.Diagnostics.ProcessThreadCollection
Processes and thread examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; namespace Examples { class Program { static void Main(string[] args) { ListAllRunningProcesses(); GetSpecificProcess(); EnumThreadsForPID(4); StartAndKillProcess(); Console.ReadLine(); } public static void ListAllRunningProcesses() { var runningProcesses = from proc in Process.GetProcesses(".") orderby proc.Id select proc; foreach (var p in runningProcesses) { string info = string.Format("->:{0}\tName:{1}", p.Id.ToString(), p.ProcessName); Console.WriteLine(info); } Console.WriteLine("***"); } public static void GetSpecificProcess() { Process p = null; try { p = Process.GetProcessById(4); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); return; } } public static void EnumThreadsForPID(int pID) { Process p = null; try { p = Process.GetProcessById(pID); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); return; } Console.WriteLine("Threads used by {0}:",p.ProcessName); ProcessThreadCollection threads = p.Threads; foreach (ProcessThread pt in threads) { string info = string.Format( "-> ThreadID:{0}\tStartTime: {1}\tPriority: {2}", pt.Id.ToString(), pt.StartTime.ToShortTimeString(), pt.PriorityLevel ); Console.WriteLine(info); } Console.WriteLine("***"); } public static void EnumModsForPID(int pID) { Process p = null; try { p = Process.GetProcessById(pID); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); return; } Console.WriteLine("Modules used by {0}:", p.ProcessName); ProcessModuleCollection modules = p.Modules; foreach (ProcessModule pm in modules) { string info =string.Format("-> ModuleName:{0}", pm.ModuleName); Console.WriteLine(info); } Console.WriteLine("***"); } public static void StartAndKillProcess() { Process p = null; try { // p = Process.Start("IExplore.exe","www.facebook.com"); // simple variant ProcessStartInfo startinfo = new ProcessStartInfo("IExplore.exe", "www.facebook.com"); startinfo.WindowStyle = ProcessWindowStyle.Maximized; p = Process.Start(startinfo); } catch (InvalidOperationException ex) { Console.WriteLine(ex.Message); } Console.WriteLine("Press Enter to kill process {0}", p.ProcessName); Console.ReadLine(); try { p.Kill(); } catch (InvalidOperationException ex) { Console.WriteLine(ex.Message); } } } } |