using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.IO; using System.Diagnostics; namespace TiesTest { static class Program { /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { if(SingleInstanceCheck() == false) { Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); frmLogin ifrmLogin = new frmLogin(); ifrmLogin.ShowDialog(); if (ifrmLogin.DialogResult.Equals(DialogResult.OK)) { ifrmLogin.Close(); Application.Run(new frmMain()); //Application.Run(frmMain.InstanceObject()); } } } static bool SingleInstanceCheck() { bool result = false; try { Process me = Process.GetCurrentProcess(); Process[] us = Process.GetProcesses(); Process another = null; foreach (var p in us) { try { if (p.Id != me.Id && p.MainModule.FileName == me.MainModule.FileName) { another = p; break; } } catch (Exception ex) { //logger.Error(ex); } } if (another != null) { MessageBox.Show("程序已运行", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Question); result = true; } } catch (Exception e) { MessageBox.Show(e.StackTrace + "\n" + e.Message); result = true; } return result; } static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { LogUnhandledException(e.Exception.Message + e.Exception.StackTrace); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { LogUnhandledException(e.ExceptionObject.ToString()); } static void LogUnhandledException(string msg) { StreamWriter sw = new StreamWriter(Application.StartupPath + "\\AppErrorLog.txt", true); sw.WriteLine(DateTime.Now.ToString() + msg); sw.Close(); } } }