我的代码:
Program.cs部分代码:
Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Login());
Login.cs部分代码:
public Login()
        {            if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1)
            {
                MessageBox.Show("okok");
                Application.Exit();
            }
            InitializeComponent();
        }
我在构造方法中判断是否为单实例,但不能防止多实例运行,怎么回事?
哪位高手帮我看看,怎么解决?

解决方案 »

  1.   


            public static Process RunningInstance()
            {
                Process current = Process.GetCurrentProcess();            Process[] processes = Process.GetProcessesByName(current.ProcessName);            //遍历正在有相同名字运行的例程
                foreach (Process process in processes)
                {
                    //忽略现有的例程
                    if (process.Id != current.Id)
                    {
                        //确保例程从EXE文件运行
                        if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
                            current.MainModule.FileName)
                        {
                            //返回另一个例程实例
                            return process;
                        }
                    }
                }            //没有其它的例程,返回Null
                return null;
            }
      

  2.   

     if ( RunningInstance() )
                { 
                    MessageBox.Show("okok"); 
                    Application.Exit(); 
                }   
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 
    http://feiyun0112.cnblogs.com/