Process extension class in C #

Source: Internet
Author: User

/// <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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.