Program.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Forms;
  8. using IntelligentControlForsx.ChildForms;
  9. using IntelligentControlForsx.Service;
  10. using IntelligentControlForsx.Service.AGC;
  11. using IntelligentControlForsx.Service.AGC.Domain;
  12. using IntelligentControlForsx.Service.WindturbineControl.IntPtrSvc;
  13. using IntelligentControlForsx.Service.ZM;
  14. using IntelligentControlForsx.Start;
  15. using IntelligentControlForsx.Test;
  16. using Gyee_Control.View;
  17. using System.Text;
  18. using log4net;
  19. using WisdomClient;
  20. using WisdomClient.data;
  21. using Application = System.Windows.Forms.Application;
  22. namespace IntelligentControlForsx
  23. {
  24. static class Program
  25. {
  26. private static ILog logger = LogManager.GetLogger("AppInfoLog");
  27. /// <summary>
  28. /// 应用程序的主入口点。
  29. /// </summary>
  30. [STAThread]
  31. static void Main()
  32. {
  33. try
  34. {
  35. //设置应用程序处理异常方式:ThreadException处理
  36. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  37. //处理UI线程异常
  38. Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
  39. //处理非UI线程异常
  40. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  41. Application.EnableVisualStyles();
  42. Application.SetCompatibleTextRenderingDefault(false);
  43. GetGycpToCache();
  44. #region 控制命令发送线程
  45. Thread sendTaskThread = new Thread(TaskSendSvc.SendCmd);
  46. sendTaskThread.IsBackground = true;
  47. sendTaskThread.Start();
  48. #endregion
  49. Application.Run(new MainWindow());
  50. }
  51. catch (Exception ex)
  52. {
  53. logger.Error(ex);
  54. //throw;
  55. }
  56. }
  57. public static void GetGycpToCache()
  58. {
  59. ControlIntPtr svc = ControlIntPtr.GetControlIntPtr();
  60. svc.init();
  61. }
  62. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  63. {
  64. string str = GetExceptionMsg(e.Exception, e.ToString());
  65. logger.Error(str);
  66. //MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  67. //LogManager.WriteLog(str);
  68. }
  69. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  70. {
  71. string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
  72. logger.Error(str);
  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. }