C#控制檯程序關閉監聽回調

static bool ConsoleEventCallback(int eventType)
        {
            if (eventType == 2)
            {
                List<Process> pList = Process.GetProcessesByName("chrome").ToList();
                try
                {
                    pList.ForEach(i => i.Kill());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            return false;
        }
        private delegate bool ConsoleEventDelegate(int eventType);
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool SetConsoleCtrlHandler(ConsoleEventDelegate callback, bool add);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章