Program.cs 3.68 KB
using Hh.Mes.Common.log;
using log4net;
using log4net.Config;

namespace HH.Data.Excel
{
    internal static class Program
    {
        /// <summary>
        /// 主窗体程序
        /// </summary>
        public static MainWindow? _mainWindow;

        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            ApplicationConfiguration.Initialize();
            AppLicationExceptionInit();
            InitLog4Net();
            AppStart();
        }

        static void AppStart()
        {
            Mutex mutex = new Mutex(true, Application.ProductName, out bool MoreAppFalg);
            if (MoreAppFalg)
            {
                _mainWindow = new MainWindow();
                Application.Run(_mainWindow);
                mutex.ReleaseMutex();
            }
            else
            {
                MessageBox.Show(null, "相同的程序已经在运行了,请不要同时运行多个程序!\n\n当前程序即将退出!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Application.Exit();
            }
        }

        #region 全局异常处理
        static void AppLicationExceptionInit()
        {
            //处理未捕获的异常
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            //ThreadException 处理UI线程异常
            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
            //UnhandledException 处理非UI线程异常
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
        }

        static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            var msg = e.Exception.Message;
            Log4NetHelper.Instance.Error(msg);
            SysMsgShow(msg);
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            // 处理UI线程的异常
            Exception ex = (Exception)e.ExceptionObject;
            var msg = "发生了一个UI线程异常。" + ex.Message;
            Log4NetHelper.Instance.Error(msg);
            SysMsgShow(msg);
            Application.Exit();
        }

        #endregion

        #region InitLog4Net
        private static void InitLog4Net()
        {
            Log4NetHelper.Instance.Repository = LogManager.CreateRepository("NETCoreRepository");
            var log4ConfigFilePath = Path.Combine(
                Path.GetDirectoryName(typeof(Program).Assembly.Location) ?? string.Empty,
                "config",
                "log4net.config"
            );
            if (!File.Exists(log4ConfigFilePath))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                SysMsgShow("log4net.config文件路径不存在,请确认配置文件路径是否正确!");
                Log4NetHelper.Instance.Info("InitLog4Net Init Failed");
            }
            else
            {
                XmlConfigurator.Configure(Log4NetHelper.Instance.Repository, new FileInfo(log4ConfigFilePath));
                Log4NetHelper.Instance.Info("InitLog4Net Init Started");
            }
        }
        #endregion

        /// <summary>
        /// 消息提醒 IOT-数据采集地址协议解析
        /// </summary>
        public static void SysMsgShow(string msg, string flag = "error")
        {
            if (flag.ToLower() == "error")
            {
                MessageBox.Show(msg, "IOT-数据采集地址协议解析-错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                MessageBox.Show(msg, "IOT-数据采集地址协议解析");
            }
        }
    }
}