1.System.Diagnostics
Process
ProcessModule
ProcessModuleCollection
ProcessStartInfo
ProcessThread
ProcessThreadCollection
2.Process
// 属性
ExitTime
Handle
Id
MachineName
MainWindowTitle
Modules
ProcessName
Responding
StartTime
Threads
// 方法
CloseMainWindow()
GetCurrentProcess()
GetProcesses()
Kill()
Start()
static void ListAllRunningProcesses()
{
var runningProcs = from proc in Process.GetProcesses(".") orderby proc.Id Select proc;
foreach(var p in runningProcs)
{
string info = string.Format("-> PID:{0}\t Name:{1}", p.Id, p.ProcessName);
Console.WriteLine(info);
}
Console.WriteLine();
}
static void EnumThreadsForPid(int pID)
{
Process theProc = null;
try
{
theProc = Process.GetProcessById(pID);
}
catch(ArgumentException ex)
{
Console.WriteLine(ex.Message);
return;
}
Console.WriteLine(theProc.ProcessName);
ProcessThreadCollection theThreads = theProc.Threads;
foreach(ProcessThread pt in theThreads)
{
string info = string.Format("{0} {1} {2}",pt.Id, pt.StartTime.ToShortTimeString(), pt.PriorityLevel);
Console.WriteLine(info);
}
}
3.ProcessThread
// 属性
CurrentPriority
Id
IdealProcessor
PriorityLevel
ProcessorAffinity
StartAddress
StartTime
ThreadState
TotalProcessorTime
WaitReason
4.Process.Modules
static void EnumModsForPid(int pID)
{
Process theProc = null;
try
{
theProc = Process.GetProcessById(pID);
}
catch(ArgumentException ex)
{
Console.WriteLine(ex.Message);
return;
}
Console.WriteLine("Loaded modules for:{0}", theProc.ProcessName);
ProcessModuleCollection theMods = theProc.Modules;
foreach(ProcessModule pm in theMods)
{
string info = string.Format("{0}", pm.ModuleName);
Console.WriteLine(info);
}
}
static void StartAndKillProcess()
{
Process ieProc = null;
try
{
ieProc = Process.Start("IExplore.exe", "www.facebook.com");
}
catch(InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
try
{
ieProc.Kill();
}
catch(InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
}
}
public sealeld class ProcessStartInfo : object
{
public ProcessStartInfo();
public ProcessStartInfo(string fileName);
public ProcessStartInfo(string fileName, string arguments);
public string Arguments{ get; set; }
public bool CreateNoWindow{ get; set; }
public StringDictionary EnvironmentVariables{ get; }
public bool ErrorDialog{ get; set; }
public IntPtr ErrorDialogParentHandle { get; set; }
public string FileName{ get; set; }
public bool LoadUserProfile { get; set; }
public SecureString Password { get; set; }
public bool RedirectStandardError { get; set; }
public bool RedirectStandardInput { get; set; }
public bool RedirectStandardOutput { get; set; }
public Encoding StandardErrorEncoding { get; set; }
public Encoding StandardOutputEncoding{ get; set; }
public bool UseShellExecute{ get; set; }
public string Verb{ get; set; }
public string[] Verbs{ get; }
public ProcessWindowStyle WindowStyle { get; set; }
public string WorkingDirectory { get; set; }
}
5.应用程序域
.NET下,可执行程序没承载在进程的一个逻辑分区中。该逻辑分区即为 应用程序域。
一个进程可包含多个应用程序域。
System.AppDomain
CreateDomain()
CreateInstance()
ExecuteAssembly()
GetAssemblies()
GetCurrentThreadId()
Load()
UnLoad()
BaseDirectory
CurrentDomain
FriendlyName
MonitoringIsEnabled
SetupInfo
AssemblyLoad
AssemblyResolve
DomainUnload
FirstChangeException
ProcessExit
UnhandledException
class Program
{
static void Main()
{
DisplayDADStats();
}
private static void DisplayDADStats()
{
AppDomain defaultAD = AppDomain.CurrentDomain;
Assembly[] loadedAssemblies = defaultAD.GetAssemblies();
foreach(Assembly a in loadedAssemblies)
{
// a.GetName().Name
// a.GetName().Version
}
}
private static void InitDAD()
{
AppDomain defaultAD = AppDomain.CurrentDomain;
defaultAD.AssemblyLoad += (o, s)=>
{
Console.WriteLine("{0}", s.LoadedAssembly.GetName().Name);
}
}
}
class Program
{
static void Main()
{
AppDomain defaultAD = AppDomain.CurrentDomain;
ListAllAssembliesInAppDomain(defaultAD);
MakeNewAppDomain();
}
private static void MakeNewAppDomain()
{
AppDomain
}
}
///
class Program
{
static void Main()
{
AppDomain defaultAD = AppDomain.CurrentDomain;
defaultAD.ProcessExit += (o, s)=>
{
//
};
ListAllAssembliesInAppDomain(defaultAD);
MakeNewAppDomain();
}
private static void MakeNewAppDomain()
{
AppDomain newAD = AppDomain.CreateDomain("SecondAppDomain");
ListAllAssembliesInAppDomain(newAD);
}
static void ListAllAssembliesInAppDomain(AppDomain ad)
{
var loadedAssemblies = from a in ad.GetAssemblies() orderby a.GetName().Name select a;
foreach(var a in loadedAssemblies)
{
// a.GetName().Name
// a.GetName().Version
}
}
private static void MakeNewAppDomain()
{
AppDomain newAD = AppDomain.CreateDomain("SecondAppDomain");
try
{
newAD.Load("CarLibrary");
}
catch(FileNotFoundException ex)
{
Console.WriteLine(ex.Message);
}
ListAllAssembliesInAppDomain(newAD);
}
}
private static void MakeNewAppDomain()
{
AppDomain newAD = AppDomain.CreateDomain("SecondAppDomain");
newAD.DomainUnload += (o, s) =>
{
Console.WriteLine("The second AppDomain has been unloaded!");
};
try
{
newAD.Load("CarLibrary");
}
catch(FileNotFoundException ex)
{
//
}
ListAllAssembliesInAppDomain(newAD);
AppDomain.Unload(newAD);
}
6.对象上下文边界
应用程序域可进一步被划分为多个上下文边界。
使用上下文,CLR可确保运行时有特殊需求的对象,可拦截进出上下文的方法调用。
每个应用程序域有一个默认的上下文。
默认的上下文用于组合对上下文没具体或唯一性需求的.NET对象。
不需要指定特定上下文的.NET类型,称为上下文灵活对象。
需要上下文分配的对象称为上下文绑定对象,须派生自System.ContextBoundObject。
这类对象,只能在其被创建的那个上下文中运行。
上下文敏感的类型也常用特定种类的.NET特性修饰。称为上下文特性。此类特性派生自ContextAttribute.
using System;
using System.Runtime.Remoting.Contexts;
using System.Threading;
class SportsCar
{
public SportsCar()
{
Context ctx = Thread.CurrentContext;
// ctx.ContextID
foreach(IContextProperty itfCtxProp in ctx.ContextProperties)
//
}
}
[Synchronization]
class SportsCarTS : ContextBoundObject
{
public SportsCarTS()
{
Context ctx = Thread.CurrentContext;
foreach(IContextProperty itfCtxProp in ctx.ContextProperties)
//
}
}