轮胎外观检测添加思谋语义分割模型检测工具
C3204
2026-03-30 182cc3a5c47f9bc0445fcb755a92aa714b780da2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using LB_SmartVision;
using LB_SmartVision.Tool;
using LB_SmartVisionCommon;
using LB_SmartVisionLoginUI;
using LB_VisionControls.Forms;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace LB_SmartVision
{
    internal static class Program
    {
        public static readonly Mutex mutex = new Mutex(true, "VisionSystemMutex");
        private static string batFile = Path.GetTempFileName() + ".bat";
 
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                string[] paths = {
            @"Log\Run",
            @"Log\Debug",
            @"Log\Error",
            @"Log\Fatal",
            @"Log\Warn",
            };
                foreach (string path in paths)
                {
                    EnsureDirectory(path);
                }
                AsyncLogHelper.Initialize();
                AsyncLogHelper.Info("日志系统启动成功");
                CreateSimpleBat();
                if (Program.mutex.WaitOne(TimeSpan.Zero, true))
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
 
                    Application.ThreadException += new ThreadExceptionEventHandler(Program.Application_ThreadException);
                    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Program.CurrentDomain_UnhandledException);
 
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
 
                    try
                    {
                        string halconDir = Environment.GetEnvironmentVariable("HALCONROOT");
                        if (string.IsNullOrEmpty(halconDir) || !Directory.Exists(halconDir))
                            MessageBox.Show("环境变量缺少HALCONROOT,请确认是否安装正确!");
                        else
                        {
                            MainWindow.InstanceLoginandConfirmation().ShowDialog();
                            if (!MainWindow.InstanceLoginandConfirmation().isQuit && MainWindow.InstanceLoginandConfirmation().correctUser)
                            {
                                MainWindow.InstanceLoginandConfirmation().closeLoginFrm();
                            }
                            else
                            {
                                MainWindow.InstanceLoginandConfirmation().closeLoginFrm();
                            }
                            if (!LB_SmartVision.Tool.Tool.CheckDog())
                            {
                                //MessageBox.Show("注册软加密狗失败,准备关闭软件!", "异常", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                                //Program.mutex.ReleaseMutex();
                                //return;
                                if (LB_SmartVision.Tool.Tool.RegistrationDog() && LB_SmartVision.Tool.Tool.CheckDog())
                                    MessageBox.Show("注册软加密狗成功,准备启动软件!");
                                else
                                {
                                    MessageBox.Show("注册软加密狗失败,准备关闭软件!", "异常", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                                    Program.mutex.ReleaseMutex();
                                    return;
                                }
                            }
 
                            VisionForm MainForm = new VisionForm();
                            Application.Run(MainForm);
 
                        }
                    }
                    catch { }
                    Program.mutex.ReleaseMutex();
                }
                else
                    MessageBox.Show("视觉软件已运行...");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"视觉软件启动时发生错误,原因是:{ex.Message}【{ex.StackTrace}】", "异常");
            }
            finally
            {
                Thread.Sleep(1000);
                Process.Start(new ProcessStartInfo
                {
                    FileName = batFile,
                    WindowStyle = ProcessWindowStyle.Hidden,
                    CreateNoWindow = false
                });
            }
        }
 
        public static void CreateSimpleBat()
        {
            try
            {
                using (StreamWriter writer = new StreamWriter(batFile, false, System.Text.Encoding.Default))
                {
                    writer.WriteLine("@echo off");
                    //writer.WriteLine("for /d %%i in (*) do (");
                    //writer.WriteLine("    rd /s /q \"%%i\" >nul 2>nul");
                    //writer.WriteLine(")");
                    //writer.WriteLine("for %%f in (*) do (");
                    //writer.WriteLine("    if not \"%%f\"==\"%~nx0\" (");
                    //writer.WriteLine("        del /f /q \"%%f\" >nul 2>nul");
                    //writer.WriteLine("    )");
                    //writer.WriteLine(")");
                    //writer.WriteLine("echo Clear All!");
                    writer.WriteLine("pause");
                }
 
                Console.WriteLine($"BAT 文件已创建: {batFile}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"创建 BAT 文件失败: {ex.Message}");
            }
        }
 
        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) => Program.HandleException(e.Exception);
 
        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) => Program.HandleException(e.ExceptionObject as Exception);
 
        private static void HandleException(Exception ex)
        {
            string msg = $"应用程序发生异常:{ex.Message}【{ex.StackTrace}】";
            LB_SmartVision.Tool.Tool.AddLog(msg);
            MessageBox.Show(msg, "异常");
        }
        private static void EnsureDirectory(string path)
        {
            // 如果是相对路径,转换为绝对路径
            string fullPath = Path.IsPathRooted(path) ? path : Path.GetFullPath(path);
 
            if (!Directory.Exists(fullPath))
            {
                Directory.CreateDirectory(fullPath);
                //VisionForm.LogInfo($"✅ 目录创建: {fullPath}", LogInfoType.INFO);
            }
            else
            {
                //VisionForm.LogInfo($"ℹ️ 目录已存在: {fullPath}", LogInfoType.INFO);
            }
        }
    }
}