Program.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Threading;
  3. using System.Windows.Forms;
  4. namespace ErpServer
  5. {
  6. static class Program
  7. {
  8. //private const string APP_NAME = "Global\\ErpServer";//ErpServer
  9. private static readonly string iniFile = Application.StartupPath + "\\config.ini";
  10. /// <summary>
  11. /// 应用程序的主入口点。
  12. /// </summary>
  13. [STAThread]
  14. static void Main()
  15. {
  16. try
  17. {
  18. Application.EnableVisualStyles();
  19. Application.SetCompatibleTextRenderingDefault(false);
  20. //处理UI线程异常
  21. Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
  22. //处理非UI线程异常
  23. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  24. string APP_NAME = INI.GetIniValue("Global", "AppName", iniFile);
  25. if (APP_NAME == "") APP_NAME = "Global\\ErpServer";
  26. else APP_NAME = "Global\\" + APP_NAME;
  27. Mutex mutex = new Mutex(false, APP_NAME);
  28. bool Running = !mutex.WaitOne(0, false);
  29. if (!Running)
  30. {
  31. Application.Run(new MainForm());
  32. }
  33. else
  34. {
  35. MessageBox.Show("程序己运行!");
  36. Environment.Exit(0);
  37. }
  38. }
  39. catch (Exception ex)
  40. {
  41. string str = "";
  42. string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "\r\n";
  43. if (ex != null)
  44. {
  45. str = string.Format(strDateInfo + "异常类型:{0}\r\n异常消息:{1}\r\n异常信息:{2}\r\n",
  46. ex.GetType().Name, ex.Message, ex.StackTrace);
  47. }
  48. else
  49. {
  50. str = string.Format("应用程序线程错误:{0}", ex);
  51. }
  52. helper.writeLog(str);
  53. Environment.Exit(-1);
  54. }
  55. finally
  56. {
  57. }
  58. }
  59. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  60. {
  61. string str = "";
  62. Exception error = e.ExceptionObject as Exception;
  63. string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "\r\n";
  64. if (error != null)
  65. {
  66. str = string.Format(strDateInfo + "Application UnhandledException:{0};\n\r堆栈信息:{1}", error.Message, error.StackTrace);
  67. }
  68. else
  69. {
  70. str = string.Format("Application UnhandledError:{0}", e);
  71. }
  72. helper.writeLog(str);
  73. Environment.Exit(-1);
  74. }
  75. private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  76. {
  77. string str = "";
  78. string strDateInfo = "出现应用程序未处理的异常:" + DateTime.Now.ToString() + "\r\n";
  79. Exception error = e.Exception as Exception;
  80. if (error != null)
  81. {
  82. str = string.Format(strDateInfo + "异常类型:{0}\r\n异常消息:{1}\r\n异常信息:{2}\r\n",
  83. error.GetType().Name, error.Message, error.StackTrace);
  84. }
  85. else
  86. {
  87. str = string.Format("应用程序线程错误:{0}", e);
  88. }
  89. helper.writeLog(str);
  90. Environment.Exit(-1);
  91. }
  92. }
  93. }