/// <Summary>
/// Process extensions
/// </Summary>
Public static class ProcessExtensions
{
# Region Functions
# Region KillProcessAsync
/// <Summary>
/// Kills a process
/// </Summary>
/// <Param name = "Process"> Process that shoshould be killed </param>
/// <Param name = "TimeToKill"> Amount of time (in MS) until the process is killed. </param>
Public static void KillProcessAsync (this Process, int TimeToKill = 0)
{
If (Process = null)
Throw new ArgumentNullException ("Process ");
ThreadPool. QueueUserWorkItem (delegate {KillProcessAsyncHelper (Process, TimeToKill );});
}
/// <Summary>
/// Kills a list of processes
/// </Summary>
/// <Param name = "Processes"> Processes that shoshould be killed </param>
/// <Param name = "TimeToKill"> Amount of time (in MS) until the processes are killed. </param>
Public static void KillProcessAsync (this IEnumerable <Process> Processes, int TimeToKill = 0)
{
If (Processes = null)
Throw new ArgumentNullException ("Processes ");
Processes. ForEach (x => ThreadPool. QueueUserWorkItem (delegate {KillProcessAsyncHelper (x, TimeToKill );}));
}
# Endregion
# Region GetInformation
/// <Summary>
/// Gets information about all processes and returns it in an HTML formatted string
/// </Summary>
/// <Param name = "Process"> Process to get information about </param>
/// <Param name = "HTMLFormat"> shocould this be HTML formatted? </Param>
/// <Returns> An HTML formatted string </returns>
Public static string GetInformation (this Process, bool HTMLFormat = true)
{
StringBuilder Builder = new StringBuilder ();
Return Builder. Append (HTMLFormat? "<Strong> ":"")
. Append (Process. ProcessName)
. Append ("Information ")
. Append (HTMLFormat? "</Strong> <br/>": "\ n ")
. Append (Process. DumpProperties (HTMLFormat ))
. Append (HTMLFormat? "<Br/>": "\ n ")
. ToString ();
}
/// <Summary>
/// Gets information about all processes and returns it in an HTML formatted string
/// </Summary>
/// <Param name = "Processes"> Processes to get information about </param>
/// <Param name = "HTMLFormat"> shocould this be HTML formatted? </Param>
/// <Returns> An HTML formatted string </returns>
Public static string GetInformation (this IEnumerable <Process> Processes, bool HTMLFormat = true)
{
StringBuilder Builder = new StringBuilder ();
Processes. ForEach (x => Builder. Append (x. GetInformation (HTMLFormat )));
Return Builder. ToString ();
}
/// <Summary>
/// Get information of the process name
/// </Summary>
/// <Param name = "Process"> Process to get information about </param>
/// <Returns> </returns>
Public static string GetInformation (this Process)
{
Return Process. ProcessName;
}
/// <Summary>
/// Gets information about all processes and returns it in string
/// </Summary>
/// <Param name = "Processes"> Processes to get information about </param>
/// <Returns> string </returns>
Public static string GetInformation (this IEnumerable <Process> Processes)
{
StringBuilder Builder = new StringBuilder ();
Processes. ForEach (x => Builder. Append (x. GetInformation ()));
Return Builder. ToString ();
}
# Endregion
# Endregion
# Region Private Static Functions
/// <Summary>
/// Kills a process asyncronously
/// </Summary>
/// <Param name = "ProcessName"> Name of the process to kill </param>
/// <Param name = "TimeToKill"> Amount of time until the process is killed </param>
Private static void KillProcessAsyncHelper (Process, int TimeToKill)
{
If (TimeToKill> 0)
Thread. Sleep (TimeToKill );
Process. Kill ();
} Www.2cto.com
# Endregion
}
From weizhiai12's column