博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
进程、线程、应用程序域
阅读量:6886 次
发布时间:2019-06-27

本文共 3535 字,大约阅读时间需要 11 分钟。

《Pro c# with .net 3.0 Sepcial Edition》

真是好书,下面的代码是我自己理解写的,做个备查。

跟书上源代码不大一样我的是写在一个项目里头的。

最后程序域的那个例子里头需要引用System.Windows.Form。

ExpandedBlockStart.gif
  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
 11ExpandedBlockStart.gif{
 12    class Program
 13ExpandedSubBlockStart.gif    {
 14        static void Main(string[] args)
 15ExpandedSubBlockStart.gif        {
 16            do
 17ExpandedSubBlockStart.gif            {
 18                try
 19ExpandedSubBlockStart.gif                {
 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)
 32ExpandedSubBlockStart.gif                {
 33                    Console.WriteLine(exc.Message);
 34                }
 35            }
 36            while (Console.ReadLine().ToUpper() != "Q");
 37        }
 38
 39        //获取当前进程集合
 40        public static void ShowAllProceses()
 41ExpandedSubBlockStart.gif        {
 42            
 43            try
 44ExpandedSubBlockStart.gif            {
 45                Process[] runningProcs = Process.GetProcesses();
 46                Console.WriteLine("Current Process Running");
 47                Console.WriteLine("**************************************************");
 48                foreach (Process p in runningProcs)
 49ExpandedSubBlockStart.gif                {
 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)
 57ExpandedSubBlockStart.gif            {
 58                Console.Write(exc.Message);
 59            }
 60
 61           
 62        }
 63
 64        //获取指定进程的线程集合
 65        public static void ShowAllThreads(int id)
 66ExpandedSubBlockStart.gif        {
 67            try
 68ExpandedSubBlockStart.gif            {
 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)
 74ExpandedSubBlockStart.gif                {
 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)
 81ExpandedSubBlockStart.gif            {
 82                Console.WriteLine(exc.Message);
 83            }
 84        }
 85
 86        //获取指定进程的模块集合
 87        public static void EnumMOdsByPid(int id)
 88ExpandedSubBlockStart.gif        {
 89            try
 90ExpandedSubBlockStart.gif            {
 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)
 96ExpandedSubBlockStart.gif                {
 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)
103ExpandedSubBlockStart.gif            {
104                Console.WriteLine(exc.Message);
105            }
106        }
107
108        //获取当前应用程序域中模块的名称和版本
109        public static void ShowAllAppDomainAssembles(AppDomain ad)
110ExpandedSubBlockStart.gif        {
111            try
112ExpandedSubBlockStart.gif            {
113                Assembly[] loadedAssembles = ad.GetAssemblies();
114                Console.WriteLine("All Assembles in currect AppDomain"+ad.FriendlyName );
115                Console.WriteLine("**********************************************");
116                foreach (Assembly a in loadedAssembles)
117ExpandedSubBlockStart.gif                {
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)
123ExpandedSubBlockStart.gif            {
124                Console.WriteLine(exc.Message);
125            }
126        }
127    }
128}
129

 本文转自today4king博客园博客,原文链接:http://www.cnblogs.com/jinzhao/archive/2008/07/24/1250456.html,如需转载请自行联系原作者

你可能感兴趣的文章
【番外篇】ASP.NET MVC快速入门之免费jQuery控件库(MVC5+EF6)
查看>>
chrome手动添加拓展
查看>>
xUtils怎样通过注解对FindViewById进行封装
查看>>
工作环境换成Ubuntu18.04小记
查看>>
Windows线程的多任务处理
查看>>
C++ 枚举类型的思考
查看>>
ARM寻址方式
查看>>
维护没有源代码的遗留 Java 项目
查看>>
早期乳腺增生案
查看>>
Android -- ViewDragHelper
查看>>
Git凭证存储(简单易懂,一学就会,认真看)
查看>>
QTP:General Error while saving the test 的解决方法
查看>>
AP_INVOICES_ALL 表结构
查看>>
android 增加Add-on属性支持的方法
查看>>
IDictionary<TKey, TValue> 接口(数据词典)讲解与示例应用
查看>>
竞争格局、本土机会、未来发展—–深度解读MIPS收购案(转)
查看>>
JS键盘码
查看>>
JS伪3D 图形透视效果
查看>>
poj 3294 Life Forms
查看>>
WCF RIA Services使用详解(转载)
查看>>