Program.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. namespace DiodesADT
  7. {
  8. static class Program
  9. {
  10. /// <summary>
  11. /// 应用程序的主入口点。
  12. /// </summary>
  13. [STAThread]
  14. static void Main()
  15. {
  16. try
  17. {
  18. //设置应用程序处理异常方式:ThreadException处理
  19. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  20. //处理UI线程异常
  21. Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
  22. //处理非UI线程异常
  23. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  24. //进程判断程序不能多开
  25. Process[] processes = Process.GetProcesses();
  26. Process currentProcess = Process.GetCurrentProcess();
  27. bool processExist = false;
  28. foreach (Process p in processes)
  29. {
  30. if (p.ProcessName == currentProcess.ProcessName && p.Id != currentProcess.Id)
  31. {
  32. processExist = true;
  33. }
  34. }
  35. if (processExist)
  36. {
  37. Application.Exit();
  38. }
  39. else
  40. {
  41. Application.EnableVisualStyles();
  42. Application.SetCompatibleTextRenderingDefault(false);
  43. //首先载入登录窗体实例
  44. LoginForm frmLogin = new LoginForm();
  45. DialogResult loginResult = frmLogin.ShowDialog();
  46. //若登录成功则加载主窗体
  47. if (loginResult == DialogResult.OK)
  48. {
  49. Application.Run(new ADTForm());
  50. }
  51. else
  52. {
  53. //登录失败则关闭当前程序进程
  54. Application.Exit();
  55. }
  56. }
  57. }
  58. catch (Exception ex)
  59. {
  60. string str = GetExceptionMsg(ex, string.Empty);
  61. MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  62. }
  63. }
  64. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  65. {
  66. string str = GetExceptionMsg(e.Exception, e.ToString());
  67. MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  68. //LogManager.WriteLog(str);
  69. }
  70. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  71. {
  72. string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
  73. MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  74. //LogManager.WriteLog(str);
  75. }
  76. /// <summary>
  77. /// 生成自定义异常消息
  78. /// </summary>
  79. /// <param name="ex">异常对象</param>
  80. /// <param name="backStr">备用异常消息:当ex为null时有效</param>
  81. /// <returns>异常字符串文本</returns>
  82. static string GetExceptionMsg(Exception ex, string backStr)
  83. {
  84. StringBuilder sb = new StringBuilder();
  85. sb.AppendLine("****************************异常文本****************************");
  86. sb.AppendLine("【出现时间】:" + DateTime.Now.ToString());
  87. if (ex != null)
  88. {
  89. sb.AppendLine("【异常类型】:" + ex.GetType().Name);
  90. sb.AppendLine("【异常信息】:" + ex.Message);
  91. sb.AppendLine("【堆栈调用】:" + ex.StackTrace);
  92. }
  93. else
  94. {
  95. sb.AppendLine("【未处理异常】:" + backStr);
  96. }
  97. sb.AppendLine("***************************************************************");
  98. return sb.ToString();
  99. }
  100. }
  101. }