Program.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using HslCommunication.LogNet;
  2. using MainForm.ClassFile.XiaomiAPI;
  3. using SqlSugar;
  4. using System;
  5. using System.Diagnostics;
  6. using System.Reflection;
  7. using System.Runtime.InteropServices;
  8. using System.Threading;
  9. using System.Windows.Forms;
  10. using WelcomeForm;
  11. namespace MainForm
  12. {
  13. /*
  14. * 注:本源码对外提供,为了方便外人解读,所以有些地方使用中文命名方法及变量
  15. * 执笔小白
  16. */
  17. static class Program
  18. {
  19. /// <summary>
  20. /// 应用程序的主入口点。
  21. /// </summary>
  22. [STAThread]
  23. static void Main()
  24. {
  25. //设置应用程序处理异常方式:ThreadException处理
  26. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  27. //处理UI线程异常
  28. Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
  29. //处理非UI线程异常
  30. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  31. Application.EnableVisualStyles();
  32. Application.SetCompatibleTextRenderingDefault(false);
  33. if (RunningInstance() == null) //判定是否已经有实例在运行,确保只运行一个实例
  34. {
  35. Form_Welcome.LoadAndRun(new Form_Main()); //没有实例在运行
  36. Environment.Exit(0);
  37. }
  38. else
  39. HandleRunningInstance(RunningInstance()); //已经有一个实例在运行
  40. }
  41. #region 在进程中查找是否已经有实例在运行,确保程序只运行一个实例
  42. [DllImport("User32.dll")]
  43. private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
  44. [DllImport("User32.dll")]
  45. public static extern bool SetForegroundWindow(System.IntPtr hWnd);
  46. private static Process RunningInstance()
  47. {
  48. Process current = Process.GetCurrentProcess();
  49. Process[] processes = Process.GetProcessesByName(current.ProcessName);
  50. foreach (Process process in processes)//遍历与当前进程名称相同的进程列表
  51. {
  52. if (process.Id != current.Id)//如果实例已经存在则忽略当前进程
  53. {
  54. //保证要打开的进程同已经存在的进程来自同一文件路径
  55. if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
  56. {
  57. return process;//返回已经存在的进程
  58. }
  59. }
  60. }
  61. return null;
  62. }
  63. //已经有了就把它激活,并将其窗口放置最前端
  64. private static void HandleRunningInstance(Process instance)
  65. {
  66. ShowWindowAsync(instance.MainWindowHandle, 1); //调用api函数,正常显示窗口
  67. SetForegroundWindow(instance.MainWindowHandle);//将窗口放置最前端
  68. }
  69. #endregion
  70. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  71. {
  72. Exception ex = e.Exception;
  73. string str = ex.StackTrace;
  74. //用于运行日志记录
  75. ILogNet logNet = new LogNetDateTime(GlobalContext.WorkLogDir, GenerateMode.ByEveryDay);
  76. logNet.WriteError("软件UI发生致命异常!异常位置:" + str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1) + ";异常信息:" + ex.Message);
  77. MessageBox.Show(e.Exception.ToString(), "软件UI发生致命异常!异常信息:" + ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
  78. }
  79. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  80. {
  81. Exception ex = e.ExceptionObject as Exception;
  82. string str = ex.StackTrace;
  83. //用于运行日志记录
  84. ILogNet logNet = new LogNetDateTime(GlobalContext.WorkLogDir, GenerateMode.ByEveryDay);
  85. logNet.WriteError("软件发生致命异常!异常位置:" + str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1) + ";异常信息:" + ex.Message);
  86. MessageBox.Show(e.ExceptionObject.ToString(), "软件发生致命异常!异常信息" + ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
  87. }
  88. }
  89. }