进程、线程、应用程序域 暑期学习笔记(一)
《Pro c# with .net 3.0 Sepcial Edition》
真是好书,下面的代码是我自己理解写的,做个备查。
跟书上源代码不大一样我的是写在一个项目里头的。
最后程序域的那个例子里头需要引用System.Windows.Form。
Code
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Diagnostics;
6using System.Reflection;
7using System.Windows.Forms;
8
9
10namespace ProcessTest
11{
12 class Program
13 {
14 static void Main(string[] args)
15 {
16 do
17 {
18 try
19 {
20 //ShowAllProceses();
21 //int id = int.Parse(Console.ReadLine());
22 //ShowAllThreads(id);
23 //EnumMOdsByPid(id);
24
25 AppDomain currectAD = AppDomain.CurrentDomain;
26 MessageBox.Show("Hello");
27 ShowAllAppDomainAssembles(currectAD);
28 AppDomain secondAppDomain = AppDomain.CreateDomain("SecondAppDoamin");
29 ShowAllAppDomainAssembles(secondAppDomain);
30 }
31 catch (Exception exc)
32 {
33 Console.WriteLine(exc.Message);
34 }
35 }
36 while (Console.ReadLine().ToUpper() != "Q");
37 }
38
39 //获取当前进程集合
40 public static void ShowAllProceses()
41 {
42
43 try
44 {
45 Process[] runningProcs = Process.GetProcesses();
46 Console.WriteLine("Current Process Running");
47 Console.WriteLine("**************************************************");
48 foreach (Process p in runningProcs)
49 {
50 Console.Write(p.Id.ToString() + "\t" + p.ProcessName.ToString());
51 Console.WriteLine();
52 }
53 //int id =int.Parse (Console.ReadLine());
54 //Process.GetProcessById(id).Kill();
55 }
56 catch (Exception exc)
57 {
58 Console.Write(exc.Message);
59 }
60
61
62 }
63
64 //获取指定进程的线程集合
65 public static void ShowAllThreads(int id)
66 {
67 try
68 {
69 Process theProc = Process.GetProcessById(id);
70 ProcessThreadCollection theThreads = theProc.Threads;
71 Console.WriteLine("All Threads of {0}", id);
72 Console.WriteLine("******************************************");
73 foreach (ProcessThread pt in theThreads)
74 {
75 string info = string.Format("-> Thread ID:{0}\tStart Time {1}\tPriority {2}", pt.Id, pt.StartTime, pt.PriorityLevel);
76 Console.WriteLine(info);
77 }
78 Console.WriteLine("This Process have {0} Threads", theProc.Threads.Count);
79 }
80 catch (Exception exc)
81 {
82 Console.WriteLine(exc.Message);
83 }
84 }
85
86 //获取指定进程的模块集合
87 public static void EnumMOdsByPid(int id)
88 {
89 try
90 {
91 Process theProc = Process.GetProcessById(id);
92 ProcessModuleCollection theModules = theProc.Modules;
93 Console.WriteLine("All Modules of {0}", id);
94 Console.WriteLine("**************************************************");
95 foreach (ProcessModule pm in theModules)
96 {
97 string info = string.Format("->{0}\tMemorySize:{1}\tFileName:{2}\tVer:{3}", pm.ModuleName, pm.ModuleMemorySize, pm.FileName, pm.FileVersionInfo);
98 Console.WriteLine(info);
99 }
100 Console.WriteLine("There are {0} Modules", theModules.Count);
101 }
102 catch (Exception exc)
103 {
104 Console.WriteLine(exc.Message);
105 }
106 }
107
108 //获取当前应用程序域中模块的名称和版本
109 public static void ShowAllAppDomainAssembles(AppDomain ad)
110 {
111 try
112 {
113 Assembly[] loadedAssembles = ad.GetAssemblies();
114 Console.WriteLine("All Assembles in currect AppDomain"+ad.FriendlyName );
115 Console.WriteLine("**********************************************");
116 foreach (Assembly a in loadedAssembles)
117 {
118 string info = string.Format("->Name:{0}\tVersion:{1}\t", a.GetName().Name ,a.GetName().Version );
119 Console.WriteLine(info);
120 }
121 }
122 catch (Exception exc)
123 {
124 Console.WriteLine(exc.Message);
125 }
126 }
127 }
128}
129
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Diagnostics;
6using System.Reflection;
7using System.Windows.Forms;
8
9
10namespace ProcessTest
11{
12 class Program
13 {
14 static void Main(string[] args)
15 {
16 do
17 {
18 try
19 {
20 //ShowAllProceses();
21 //int id = int.Parse(Console.ReadLine());
22 //ShowAllThreads(id);
23 //EnumMOdsByPid(id);
24
25 AppDomain currectAD = AppDomain.CurrentDomain;
26 MessageBox.Show("Hello");
27 ShowAllAppDomainAssembles(currectAD);
28 AppDomain secondAppDomain = AppDomain.CreateDomain("SecondAppDoamin");
29 ShowAllAppDomainAssembles(secondAppDomain);
30 }
31 catch (Exception exc)
32 {
33 Console.WriteLine(exc.Message);
34 }
35 }
36 while (Console.ReadLine().ToUpper() != "Q");
37 }
38
39 //获取当前进程集合
40 public static void ShowAllProceses()
41 {
42
43 try
44 {
45 Process[] runningProcs = Process.GetProcesses();
46 Console.WriteLine("Current Process Running");
47 Console.WriteLine("**************************************************");
48 foreach (Process p in runningProcs)
49 {
50 Console.Write(p.Id.ToString() + "\t" + p.ProcessName.ToString());
51 Console.WriteLine();
52 }
53 //int id =int.Parse (Console.ReadLine());
54 //Process.GetProcessById(id).Kill();
55 }
56 catch (Exception exc)
57 {
58 Console.Write(exc.Message);
59 }
60
61
62 }
63
64 //获取指定进程的线程集合
65 public static void ShowAllThreads(int id)
66 {
67 try
68 {
69 Process theProc = Process.GetProcessById(id);
70 ProcessThreadCollection theThreads = theProc.Threads;
71 Console.WriteLine("All Threads of {0}", id);
72 Console.WriteLine("******************************************");
73 foreach (ProcessThread pt in theThreads)
74 {
75 string info = string.Format("-> Thread ID:{0}\tStart Time {1}\tPriority {2}", pt.Id, pt.StartTime, pt.PriorityLevel);
76 Console.WriteLine(info);
77 }
78 Console.WriteLine("This Process have {0} Threads", theProc.Threads.Count);
79 }
80 catch (Exception exc)
81 {
82 Console.WriteLine(exc.Message);
83 }
84 }
85
86 //获取指定进程的模块集合
87 public static void EnumMOdsByPid(int id)
88 {
89 try
90 {
91 Process theProc = Process.GetProcessById(id);
92 ProcessModuleCollection theModules = theProc.Modules;
93 Console.WriteLine("All Modules of {0}", id);
94 Console.WriteLine("**************************************************");
95 foreach (ProcessModule pm in theModules)
96 {
97 string info = string.Format("->{0}\tMemorySize:{1}\tFileName:{2}\tVer:{3}", pm.ModuleName, pm.ModuleMemorySize, pm.FileName, pm.FileVersionInfo);
98 Console.WriteLine(info);
99 }
100 Console.WriteLine("There are {0} Modules", theModules.Count);
101 }
102 catch (Exception exc)
103 {
104 Console.WriteLine(exc.Message);
105 }
106 }
107
108 //获取当前应用程序域中模块的名称和版本
109 public static void ShowAllAppDomainAssembles(AppDomain ad)
110 {
111 try
112 {
113 Assembly[] loadedAssembles = ad.GetAssemblies();
114 Console.WriteLine("All Assembles in currect AppDomain"+ad.FriendlyName );
115 Console.WriteLine("**********************************************");
116 foreach (Assembly a in loadedAssembles)
117 {
118 string info = string.Format("->Name:{0}\tVersion:{1}\t", a.GetName().Name ,a.GetName().Version );
119 Console.WriteLine(info);
120 }
121 }
122 catch (Exception exc)
123 {
124 Console.WriteLine(exc.Message);
125 }
126 }
127 }
128}
129