ProcessManager类的解读

来源:互联网 发布:音乐相册制作软件排名 编辑:程序博客网 时间:2024/06/05 14:16
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;


namespace NodeClient.Utils
{
    public class ProcessManager
    {
        public static bool existsProcess(String processName)
        {
            var p = Process.GetProcessesByName(processName);
            return p.Length > 0;
        }


        public static void killProcess(String processName)
        {
            var p = Process.GetProcessesByName(processName);
            foreach (var process in p)
            {
                process.Kill();
                var currentTime = DateTime.Now;
                while (true)
                {
                    try
                    {
                        if (process.HasExited)
                        {
                            return;
                        }
                        else if (currentTime.AddSeconds(5) > DateTime.Now)
                        {
                            Utils.Logger.Error(typeof(ProcessManager), "Kill Process longer than 5 s:" + existsProcess(processName).ToString());
                            return;
                        }
                        else
                        {
                            Thread.Sleep(20);
                        }
                    }
                    catch (Exception e)
                    {
                        Utils.Logger.Warn(typeof(ProcessManager), "Kill process catch exception:" + e.ToString());
                    }
                }
            }
        }


        public static Process startProcess(string FileName, string Arguments = null, string targetDir = null, bool adminAcess = false, bool hideWindow = false, bool waitExist = false)
        {
            try
            {
                Process proc = new Process();
                if (!string.IsNullOrEmpty(targetDir))
                {
                    proc.StartInfo.WorkingDirectory = targetDir;
                }
                proc.StartInfo.FileName = FileName;
                if (!string.IsNullOrEmpty(Arguments))
                {
                    proc.StartInfo.Arguments = Arguments;
                }
                if (adminAcess)
                {
                    proc.StartInfo.UseShellExecute = true;
                    proc.StartInfo.Verb = "runas";
                }
                if (hideWindow)
                {
                    proc.StartInfo.CreateNoWindow = true;
                    proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                }
                Logger.Info(typeof(ProcessManager), FileName + " Process start.");
                proc.Start();
                if (waitExist)
                {
                    proc.WaitForExit();
                    Logger.Info(typeof(ProcessManager), FileName + " Process finished.");
                }


                return proc;
            }
            catch (Exception ex)
            {
                Logger.Error(typeof(ProcessManager), FileName + " Process start Failed.");
                throw ex;
            }
        }


        internal const int CTRL_C_EVENT = 0;
        [DllImport("kernel32.dll")]
        internal static extern bool GenerateConsoleCtrlEvent(uint dwCtrlEvent, uint dwProcessGroupId);
        [DllImport("kernel32.dll", SetLastError = true)]
        internal static extern bool AttachConsole(uint dwProcessId);
        [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
        internal static extern bool FreeConsole();
        [DllImport("kernel32.dll")]
        static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate HandlerRoutine, bool Add);
        // Delegate type to be used as the Handler Routine for SCCH
        delegate bool ConsoleCtrlDelegate(uint CtrlType);


        public static void cancelConsoleProcess(Process process)
        {
            if (AttachConsole((uint)process.Id))
            {
                SetConsoleCtrlHandler(null, true);
                try
                {
                    if (!GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0))
                    {
                        process.Kill();
                        return;
                    }
                    process.WaitForExit();
                }
                finally
                {
                    FreeConsole();
                    SetConsoleCtrlHandler(null, false);
                }
            }
            else
            {
                process.Kill();
            }
        }
    }
}
原创粉丝点击