private static void Application_Idle(object sender, EventArgs e) { Application.Idle -= new EventHandler(Application_Idle); if (context.MainForm == null) { frmMain mf = new frmMain(); context.MainForm = mf; frmStart sf = (frmStart)context.Tag; sf.label3.Text = "正在初始化中....."; mf.FormInit(); sf.label3.Text = "初始化成功....."; Application.DoEvents(); sf.Close(); //关闭启动窗体 sf.Dispose(); mf.Show(); //启动主程序窗体 } }
static void Main() { try { bool canCreate = false; Mutex mutex = new Mutex(true, "79AA56EC-00DE-478d-8717-FD873BD10870", out canCreate); if (!canCreate) { MessageBox.Show("程序已经在运行!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); //处理UI线程异常 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); //处理非UI线程异常 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); frmStart sf = new frmStart(); sf.Show(); context = new ApplicationContext(); context.Tag = sf; Application.Idle += new EventHandler(Application_Idle); //注册程序运行空闲去执行主程序窗体相应初始化代码 Application.Run(context); } catch (Exception ex) { string str = ""; string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "\r\n"; if (ex != null) { str = string.Format(strDateInfo + "异常类型:{0}\r\n异常消息:{1}\r\n异常信息:{2}\r\n", ex.GetType().Name, ex.Message, ex.StackTrace); } else { str = string.Format("应用程序线程错误:{0}", ex); } DMSFrame.Loggers.LoggerManager.FileLogger.LogWithTime(str); MessageBox.Show("发生致命错误,请及时联系作者!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }