winform 进程唯一,打开第二个激活第一个进程的窗体显示

   static class Program
    {
        public static EventWaitHandle ProgramStarted;
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            // 尝试创建一个命名事件
            bool isOnly;
            ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, "OnlyStartEvent", out isOnly);

            // 如果该命名事件已经存在(存在有前一个运行实例),则发事件通知并退出
            if (!isOnly)
            {
                ProgramStarted.Set();
                return;
            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //处理未捕获的异常   
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            //处理UI线程异常   
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            //处理非UI线程异常   
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Application.ApplicationExit += new EventHandler(Application_ApplicationExit);


            Application.Run(new MainForm());
        }
    }

  public MainForm()
        {
            InitializeComponent();
            ThreadPool.RegisterWaitForSingleObject(Program.ProgramStarted, OnProgramStarted, null, -1, false);
            this.skinEngine1.SkinFile = AppDomain.CurrentDomain.BaseDirectory + "Skin/Skin.ssk";
        }

 

posted on 2015-06-04 10:24  酒歌  阅读(753)  评论(0编辑  收藏  举报