[置頂] C#中 Process的擴充類ProcessExtensions

來源:互聯網
上載者:User
/// <summary>
    /// Process extensions
    /// </summary>
    public static class ProcessExtensions
    {
        #region Functions

        #region KillProcessAsync

        /// <summary>
        /// Kills a process
        /// </summary>
        /// <param name="Process">Process that should be killed</param>
        /// <param name="TimeToKill">Amount of time (in ms) until the process is killed.</param>
        public static void KillProcessAsync(this Process 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 should 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">Should this be HTML formatted?</param>
        /// <returns>An HTML formatted string</returns>
        public static string GetInformation(this Process 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">Should 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 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 Process, int TimeToKill)
        {
            if (TimeToKill > 0)
                Thread.Sleep(TimeToKill);
            Process.Kill();
        }

        #endregion
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.