C3032
2026-01-09 987241c46c5f76996cc3c77e570d795fdc984113
LB_SmartVision/Program.cs
@@ -1,3 +1,6 @@
using LB_SmartVisionLoginUI;
using System.Text;
namespace LB_SmartVision
{
    internal static class Program
@@ -8,10 +11,76 @@
        [STAThread]
        static void Main()
        {
            // To customize application configuration such as set high DPI settings or default font,
            // see https://aka.ms/applicationconfiguration.
            ApplicationConfiguration.Initialize();
            Application.Run(new VisionForm());
            try
            {
                string halconDir = Environment.GetEnvironmentVariable("HALCONROOT");
                if (string.IsNullOrEmpty(halconDir) || !Directory.Exists(halconDir))
                {
                    System.Windows.Forms.MessageBox.Show("环境变量缺少HALCONROOT,请确认是否安装正确!");
                }
                else
                {
                    bool ret;
                    System.Threading.Mutex mutex = new System.Threading.Mutex(true, System.Windows.Forms.Application.ProductName, out ret);
                    if (ret)
                    {
                        ////登录验证权限
                        MainWindow.InstanceLoginandConfirmation().ShowDialog();
                        if (!MainWindow.InstanceLoginandConfirmation().isQuit && MainWindow.InstanceLoginandConfirmation().correctUser)
                        {
                            MainWindow.InstanceLoginandConfirmation().closeLoginFrm();
                        }
                        else
                        {
                            MainWindow.InstanceLoginandConfirmation().closeLoginFrm();
                        }
                        ////   Main   为你程序的主窗体,如果是控制台程序不用这句
                        Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                        System.Windows.Forms.Application.EnableVisualStyles();
                        System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
                        System.Windows.Forms.Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                        System.Windows.Forms.Application.ThreadException += (sender, e) => HandleException(e.Exception);
                        AppDomain.CurrentDomain.UnhandledException += (sender, e) => HandleException(e.ExceptionObject as Exception);
                        //// To customize application configuration such as set high DPI settings or default font,
                        //// see https://aka.ms/applicationconfiguration.
                        System.Windows.Forms.Application.Run(new VisionForm());
                        mutex.ReleaseMutex();
                    }
                    else
                    {
                        System.Windows.Forms.MessageBox.Show("有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\r\n这个程序即将退出。", System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        //   提示信息,可以删除。
                        System.Windows.Forms.Application.Exit();//退出程序
                    }
                }
            }
            catch (Exception ex)
            {
                if (null != ex)
                {
                    string strMsg = "InitControl failed. Error Code: " + ex.Message;
                    System.Windows.Forms.MessageBox.Show(strMsg);
                }
                else
                {
                    return;
                }
            }
        }
        static void HandleException(Exception ex)
        {
            // 记录异常信息
            string logPath = @"\Logs\app_crash.log";
            Directory.CreateDirectory(Path.GetDirectoryName(logPath));
            File.AppendAllText(logPath, $"{DateTime.Now}: {ex}\n");
            // 显示错误信息(可选)
            System.Windows.Forms.MessageBox.Show($"程序发生错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            // 退出程序
            Environment.Exit(1);
        }
    }
}