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";
        }
相關文章
相關標籤/搜索