当前位置:首页 > Windows程序 > 正文

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

2021-05-26 Windows程序

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()); } }

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/71323.html