码迷,mamicode.com
首页 > Windows程序 > 详细

C#如何关闭指定进程

时间:2018-12-13 13:50:19      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:ready   win   pre   mes   cat   ali   进程   env   term   

 public static void KillProcess(string strProcessesByName)//关闭线程
        {
            foreach (Process p in Process.GetProcesses())//GetProcessesByName(strProcessesByName))
            {
                if (p.ProcessName.ToUpper().Contains("你要关闭的进程名字"))
                {
                    try
                    {
                        p.Kill();
                        p.WaitForExit(); // possibly with a timeout
                    }
                    catch (Win32Exception e)
                    {
                        MessageBox.Show(e.Message.ToString());   // process was terminating or can‘t be terminated - deal with it
                    }
                    catch (InvalidOperationException e)
                    {
                        MessageBox.Show(e.Message.ToString()); // process has already exited - might be able to let this one go
                    }
                }
            }
        }

或者这样关闭自己

 Environment.Exit(0);

 

C#如何关闭指定进程

标签:ready   win   pre   mes   cat   ali   进程   env   term   

原文地址:https://www.cnblogs.com/snow-zhang/p/10113460.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!