123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using HslCommunication.LogNet;
- using MainForm.ClassFile.XiaomiAPI;
- using SqlSugar;
- using System;
- using System.Diagnostics;
- using System.Reflection;
- using System.Runtime.InteropServices;
- using System.Threading;
- using System.Windows.Forms;
- using WelcomeForm;
- namespace MainForm
- {
- /*
- * 注:本源码对外提供,为了方便外人解读,所以有些地方使用中文命名方法及变量
- * 执笔小白
- */
- static class Program
- {
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- //设置应用程序处理异常方式:ThreadException处理
- Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
- //处理UI线程异常
- Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
- //处理非UI线程异常
- AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- if (RunningInstance() == null) //判定是否已经有实例在运行,确保只运行一个实例
- {
- Form_Welcome.LoadAndRun(new Form_Main()); //没有实例在运行
- Environment.Exit(0);
- }
- else
- HandleRunningInstance(RunningInstance()); //已经有一个实例在运行
- }
- #region 在进程中查找是否已经有实例在运行,确保程序只运行一个实例
- [DllImport("User32.dll")]
- private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
- [DllImport("User32.dll")]
- public static extern bool SetForegroundWindow(System.IntPtr hWnd);
- private static Process RunningInstance()
- {
- Process current = Process.GetCurrentProcess();
- Process[] processes = Process.GetProcessesByName(current.ProcessName);
- foreach (Process process in processes)//遍历与当前进程名称相同的进程列表
- {
- if (process.Id != current.Id)//如果实例已经存在则忽略当前进程
- {
- //保证要打开的进程同已经存在的进程来自同一文件路径
- if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
- {
- return process;//返回已经存在的进程
- }
- }
- }
- return null;
- }
- //已经有了就把它激活,并将其窗口放置最前端
- private static void HandleRunningInstance(Process instance)
- {
- ShowWindowAsync(instance.MainWindowHandle, 1); //调用api函数,正常显示窗口
- SetForegroundWindow(instance.MainWindowHandle);//将窗口放置最前端
- }
- #endregion
- static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
- {
- Exception ex = e.Exception;
- string str = ex.StackTrace;
- //用于运行日志记录
- ILogNet logNet = new LogNetDateTime(GlobalContext.WorkLogDir, GenerateMode.ByEveryDay);
- logNet.WriteError("软件UI发生致命异常!异常位置:" + str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1) + ";异常信息:" + ex.Message);
- MessageBox.Show(e.Exception.ToString(), "软件UI发生致命异常!异常信息:" + ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
- {
- Exception ex = e.ExceptionObject as Exception;
- string str = ex.StackTrace;
- //用于运行日志记录
- ILogNet logNet = new LogNetDateTime(GlobalContext.WorkLogDir, GenerateMode.ByEveryDay);
- logNet.WriteError("软件发生致命异常!异常位置:" + str.Substring(str.LastIndexOf("\\") + 1, str.Length - str.LastIndexOf("\\") - 1) + ";异常信息:" + ex.Message);
- MessageBox.Show(e.ExceptionObject.ToString(), "软件发生致命异常!异常信息" + ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- }
|