进程、线程、应用程序域

简介:

《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,如需转载请自行联系原作者

相关文章
|
6天前
|
存储 消息中间件 资源调度
「offer来了」进程线程有啥关系?10个知识点带你巩固操作系统基础知识
该文章总结了操作系统基础知识中的十个关键知识点,涵盖了进程与线程的概念及区别、进程间通信方式、线程同步机制、死锁现象及其预防方法、进程状态等内容,并通过具体实例帮助理解这些概念。
「offer来了」进程线程有啥关系?10个知识点带你巩固操作系统基础知识
|
5天前
|
资源调度 算法 调度
深入浅出操作系统之进程与线程管理
【9月更文挑战第29天】在数字世界的庞大舞台上,操作系统扮演着不可或缺的角色,它如同一位精通多门艺术的导演,精心指挥着每一个进程和线程的演出。本文将通过浅显的语言,带你走进操作系统的内心世界,探索进程和线程的管理奥秘,让你对这位幕后英雄有更深的了解。
|
9天前
|
Java
直接拿来用:进程&进程池&线程&线程池
直接拿来用:进程&进程池&线程&线程池
|
10天前
|
负载均衡 Java 调度
探索Python的并发编程:线程与进程的比较与应用
本文旨在深入探讨Python中的并发编程,重点比较线程与进程的异同、适用场景及实现方法。通过分析GIL对线程并发的影响,以及进程间通信的成本,我们将揭示何时选择线程或进程更为合理。同时,文章将提供实用的代码示例,帮助读者更好地理解并运用这些概念,以提升多任务处理的效率和性能。
|
20天前
|
开发者 Python
深入浅出操作系统:进程与线程的奥秘
【8月更文挑战第46天】在数字世界的幕后,操作系统扮演着至关重要的角色。本文将揭开进程与线程这两个核心概念的神秘面纱,通过生动的比喻和实际代码示例,带领读者理解它们的定义、区别以及如何在编程中运用这些知识来优化软件的性能。无论你是初学者还是有一定经验的开发者,这篇文章都将为你提供新的视角和实用技巧。
|
4天前
|
数据采集 消息中间件 并行计算
进程、线程与协程:并发执行的三种重要概念与应用
进程、线程与协程:并发执行的三种重要概念与应用
15 0
|
4天前
|
数据采集 Linux 调度
Python之多线程与多进程
Python之多线程与多进程
11 0
|
9天前
|
存储 算法 Java
关于python3的一些理解(装饰器、垃圾回收、进程线程协程、全局解释器锁等)
该文章深入探讨了Python3中的多个重要概念,包括装饰器的工作原理、垃圾回收机制、进程与线程的区别及全局解释器锁(GIL)的影响等,并提供了详细的解释与示例代码。
15 0
|
12天前
|
并行计算 API 调度
探索Python中的并发编程:线程与进程的对比分析
【9月更文挑战第21天】本文深入探讨了Python中并发编程的核心概念,通过直观的代码示例和清晰的逻辑推理,引导读者理解线程与进程在解决并发问题时的不同应用场景。我们将从基础理论出发,逐步过渡到实际案例分析,旨在揭示Python并发模型的内在机制,并比较它们在执行效率、资源占用和适用场景方面的差异。文章不仅适合初学者构建并发编程的基础认识,同时也为有经验的开发者提供深度思考的视角。
|
17天前
|
消息中间件 程序员 数据处理
探究操作系统中的进程间通信(IPC)机制及其在现代软件开发中的应用
本文深入探讨了操作系统中的核心概念——进程间通信(IPC),揭示了其在现代软件开发中的关键作用。通过对各种IPC机制如管道、消息队列、共享内存等的详细分析,本文旨在为读者提供一个清晰的理解框架,帮助他们掌握如何在实际应用中有效利用这些技术以实现进程间的协同工作。此外,文章还将探讨IPC在高并发环境下的性能优化策略,以及如何避免常见的IPC编程错误。通过结合理论与实践,本文不仅适合希望深入了解操作系统原理的技术人员阅读,也对那些致力于提升软件质量和开发效率的程序员具有重要参考价值。
19 0

热门文章

最新文章

相关实验场景

更多
下一篇
无影云桌面