Program.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Windows.Forms;
  6. using System.Threading;
  7. using System.Diagnostics;
  8. namespace LYFZ.ManagementService
  9. {
  10. static class Program
  11. {
  12. /// <summary>
  13. /// 应用程序的主入口点。
  14. /// </summary>
  15. [STAThread]
  16. static void Main()
  17. {
  18. Application.EnableVisualStyles();
  19. Application.SetCompatibleTextRenderingDefault(false);
  20. //处理未捕获的异常
  21. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  22. //处理UI线程异常
  23. Application.ThreadException += Application_ThreadException;
  24. //处理非UI线程异常
  25. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  26. bool create;
  27. using (Mutex mu = new Mutex(true, Application.ProductName, out create))
  28. {
  29. if (create)
  30. {
  31. Application.Run(new frmMain());
  32. }
  33. else
  34. {
  35. MessageBox.Show("程序已经在运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  36. Application.Exit();
  37. return;
  38. }
  39. }
  40. }
  41. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  42. {
  43. Exception myException = (Exception)e.ExceptionObject;
  44. try
  45. {
  46. LYFZ.ComponentLibrary.frmExceptionError frmError = new ComponentLibrary.frmExceptionError();
  47. frmError.Text = "未捕获的错误";
  48. frmError.ErrorTypeName = "[" + frmError.Text + "]" + myException.GetType().Name;
  49. frmError.ErrorMessage = myException.Message;
  50. frmError.ErrorStackTrace = myException.StackTrace.ToString();
  51. frmError.ShowDialog();
  52. errorWriteSystemLog(frmError.txtErrorInfo.Text);
  53. }
  54. catch
  55. {
  56. MessageBox.Show("未被捕获的异常错误,\r\n错误原因:" + myException.Message);
  57. }
  58. // Application.ExitThread();
  59. // Application.Exit();
  60. }
  61. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  62. {
  63. try
  64. {
  65. Exception myException = e.Exception;
  66. LYFZ.ComponentLibrary.frmExceptionError frmError = new ComponentLibrary.frmExceptionError();
  67. frmError.Text = "未捕获的UI线程错误";
  68. frmError.ErrorTypeName = "[" + frmError.Text + "]" + myException.GetType().Name;
  69. frmError.ErrorMessage = myException.Message;
  70. frmError.ErrorStackTrace = myException.StackTrace.ToString();
  71. frmError.ShowDialog();
  72. errorWriteSystemLog(frmError.txtErrorInfo.Text);
  73. }
  74. catch
  75. {
  76. MessageBox.Show("未捕获的线程异常错误,\r\n错误原因:" + e.Exception.Message.ToString());
  77. }
  78. }
  79. static void errorWriteSystemLog(string errorMsg)
  80. {
  81. try
  82. {
  83. LYFZ.DAL.DAL_FileLogs.WriteMainLogs("程序错误日志:" + errorMsg);
  84. }
  85. catch { }
  86. }
  87. }
  88. }