zoukankan      html  css  js  c++  java
  • 监测进程

    [DllImport("kernel32.dll")]
            private static extern int Process32First(IntPtr hSnapshot,
                                             ref PROCESSENTRY32 lppe);

            [DllImport("kernel32.dll")]
            private static extern int Process32Next(IntPtr hSnapshot,
                                            ref PROCESSENTRY32 lppe);

            [DllImport("kernel32.dll", SetLastError = true)]
            private static extern IntPtr CreateToolhelp32Snapshot(uint dwFlags,
                                                           uint th32ProcessID);

            [DllImport("kernel32.dll", SetLastError = true)]
            private static extern bool CloseHandle(IntPtr hSnapshot);

            private const uint TH32CS_SNAPPROCESS = 0x00000002;

            [StructLayout(LayoutKind.Sequential)]
            private struct PROCESSENTRY32
            {
                public uint dwSize;
                public uint cntUsage;
                public uint th32ProcessID;
                public IntPtr th32DefaultHeapID;
                public uint th32ModuleID;
                public uint cntThreads;
                public uint th32ParentProcessID;
                public int pcPriClassBase;
                public uint dwFlags;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
                public string szExeFile;
            }
            public static bool IsProcessRunning(string applicationName,ref int count)
            {
                IntPtr handle = IntPtr.Zero;
                try
                {
                    handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                    PROCESSENTRY32 info = new PROCESSENTRY32();
                    info.dwSize = (uint)System.Runtime.InteropServices.
                                  Marshal.SizeOf(typeof(PROCESSENTRY32));
                    int first = Process32First(handle, ref info);
                    do
                    {
                        //Console.WriteLine(info.szExeFile);
                        if (string.Compare(info.szExeFile,
                              applicationName, true) == 0)
                        {
                            count++;
                        }
                    }
                    while (Process32Next(handle, ref info) != 0);
                }
                catch
                {
                    throw;
                }
                finally
                {
                    CloseHandle(handle);
                    handle = IntPtr.Zero;
                }
                return false;
            }


            #region 强制关闭进程
            /// <summary>
            /// 关闭进程
            /// </summary>
            /// <param name="processName">进程名</param>
            private static void KillProcess(string processName)
            {
                System.Diagnostics.Process[] myproc = System.Diagnostics.Process.GetProcessesByName(processName);
                foreach (var item in myproc)
                {
                    if (item.ProcessName == processName)
                    {
                        item.Kill();
                    }
                }

            }



            //强制关闭最近打开的某个进程

            private static void KillRecentProcess(string processName)
            {
                System.Diagnostics.Process[] Proc = System.Diagnostics.Process.GetProcessesByName(processName);
                System.DateTime startTime = new DateTime();
                int m, killId = 0;
                for (m = 0; m < Proc.Length; m++)
                {
                    if (startTime < Proc[m].StartTime)
                    {
                        startTime = Proc[m].StartTime;
                        killId = m;
                    }
                }
                if (Proc[killId].HasExited == false)
                {
                    Proc[killId].Kill();
                }
            }
            #endregion

  • 相关阅读:
    Eth-Trunk 技术
    MSTP技术
    STP生成树原理及不足
    表示数值的字符串(python)
    字符流中第一个不重复的字符(python)
    连续子数组的最大和(python)
    和为S的两个数字(python)
    数组中重复的数字(python)
    构建乘积数组(python)
    idea中查看类层级class hierarchy
  • 原文地址:https://www.cnblogs.com/jinhaoObject/p/5242648.html
Copyright © 2011-2022 走看看