当前位置: 代码迷 >> C# >> winform二次启动后,关闭前一个进程的有关问题
  详细解决方案

winform二次启动后,关闭前一个进程的有关问题

热度:54   发布时间:2016-05-05 04:09:30.0
winform二次启动后,关闭前一个进程的问题

  static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Process[] process = Process.GetProcessesByName("_SCREEN_CAPTURE_TOOL");
            foreach (Process p in process)
            {
                p.CloseMainWindow();
            }
            
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

请教,需求是带参数启动exe,当下一次启动exe时,需要关闭上一次启动的进程
遇到的问题,使用Process.CloseMainWindow()时,不能关闭处于hide状态的窗口,只能关闭处于active的窗口
而如果用Process.kill(),则只会杀掉之前的进程,不能重新打开新的进程
PS:有没有其他的方法能实现进程获取新的参数?除了进程间通信(SendMessage)
------解决思路----------------------


           Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);

            //Loop through  the running processes in with the same name    

            foreach (Process process in processes)
            {
                //Ignore   the   current   process   
                if (process.Id != current.Id)
                {

                     process.Kill();
                }
            }

  相关解决方案