151 lines
5.3 KiB
C#
Raw Normal View History

2025-03-07 09:06:46 +08:00
using System;
2025-04-08 16:17:34 +08:00
using System.Data.Entity;
2025-03-07 09:06:46 +08:00
using System.Drawing;
2025-04-08 16:17:34 +08:00
using System.Reflection;
2025-03-07 09:06:46 +08:00
using System.Windows.Forms;
2025-03-27 12:07:23 +08:00
using AntdUI;
using DH.Commons.Base;
2025-03-27 12:07:23 +08:00
using DH.Commons.Enums;
using DH.Commons.Helper;
using DH.Commons.Models;
using DH.RBAC.Common;
using DH.RBAC.Logic.Base;
using DH.RBAC.Utility.Extension;
using DH.RBAC.Utility.Other;
2025-03-24 15:20:33 +08:00
using DHSoftware.Utils;
2025-03-07 09:06:46 +08:00
using DHSoftware.Views;
using Microsoft.VisualBasic.Logging;
2025-04-08 16:17:34 +08:00
using Newtonsoft.Json;
using GlobalConfig = DH.RBAC.Common.GlobalConfig;
2025-03-07 09:06:46 +08:00
namespace DHSoftware
{
internal static class Program
{
2025-04-08 16:17:34 +08:00
2025-03-07 09:06:46 +08:00
private static MainWindow mainWindow;
2025-04-08 16:17:34 +08:00
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
2025-03-07 09:06:46 +08:00
static void Main()
{
// 必须在第一个窗口创建前调用以下两行
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// 注册全局异常处理
2025-03-07 09:06:46 +08:00
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
// 初始化AntdUI配置
2025-03-07 09:06:46 +08:00
AntdUI.Localization.DefaultLanguage = "zh-CN";
AntdUI.Config.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
AntdUI.Config.SetCorrectionTextRendering("Microsoft YaHei UI");
2025-03-24 15:20:33 +08:00
AntdUI.Style.Set(AntdUI.Colour.Primary, Color.FromArgb(46, 108, 227));
// 现在再创建窗口
WelcomeWindow.Instance.Show();
UpdateStep(0, "正在初始化", true);
UpdateStep(10, "正在加载数据库", true);
2025-03-27 12:07:23 +08:00
try
{
2025-04-08 16:17:34 +08:00
MyConfig config = File.ReadAllText(MyEnvironment.RootPath("db/config.json")).ToObject<MyConfig>();
GlobalConfig.Config = config;
string message = "";
bool flag = BaseLogic.InitDB(config.DbType, config.DbHost, config.DbName, config.DbUserName, config.DbPassword, ref message);
2025-04-08 16:17:34 +08:00
if (!flag)
{
Console.Write(message);
return;
}
//DatabaseUtil.InitializeDatabase();
2025-03-27 12:07:23 +08:00
}
catch (Exception ex)
{
SystemModel.CurrentStatus = EnumStatus.;
Modal.open(WelcomeWindow.Instance, "错误!", ex.ToString(), TType.Error);
2025-03-27 12:07:23 +08:00
}
UpdateStep(30, "正在加载解决方案", true);
2025-03-27 12:07:23 +08:00
try
{
MainWindow.Instance.LoadScheme();
}
catch (Exception ex)
{
SystemModel.CurrentStatus = EnumStatus.;
Modal.open(WelcomeWindow.Instance, "错误!", ex.ToString(), TType.Error);
}
UpdateStep(50, "正在连接相机", true);
2025-03-27 12:07:23 +08:00
try
{
MainWindow.Instance.ConnectCamera();
}
catch(Exception ex)
{
SystemModel.CurrentStatus = EnumStatus.;
Modal.open(WelcomeWindow.Instance, "错误!", ex.ToString(), TType.Error);
}
UpdateStep(70, "正在连接PLC", true);
2025-03-27 12:07:23 +08:00
try
{
MainWindow.Instance.ConnectPLC();
}
catch (Exception ex)
{
SystemModel.CurrentStatus = EnumStatus.;
Modal.open(WelcomeWindow.Instance, "错误!", ex.ToString(), TType.Error);
}
UpdateStep(80, "正在加载算法模型", true);
2025-03-27 12:07:23 +08:00
try
{
MainWindow.Instance.InitModel();
}
catch (Exception ex)
{
SystemModel.CurrentStatus = EnumStatus.;
Modal.open(WelcomeWindow.Instance, "错误!", ex.ToString(), TType.Error);
}
UpdateStep(100, "程序初始化完成", true);
Thread.Sleep(100);
WelcomeWindow.Instance.Close();
// 启动主窗口
Application.Run(MainWindow.Instance);
2025-03-07 09:06:46 +08:00
}
// 捕获UI线程中的未处理异常
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
AntdUI.Notification.error(mainWindow, "未处理的UI线程异常", e.Exception.Message, autoClose: 3, align: AntdUI.TAlignFrom.TR);
}
// 捕获非UI线程中的未处理异常
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
AntdUI.Notification.error(mainWindow, "未处理的非UI线程异常", e.ToString(), autoClose: 3, align: AntdUI.TAlignFrom.TR);
}
//更新进度
internal static void UpdateStep(int percentValue, string stepMsg, bool succeed)
{
try
{
WelcomeWindow.Instance.bar_step.Value = percentValue;
WelcomeWindow.Instance.lbl_step.Text = stepMsg + "......";
Thread.Sleep(200);
Application.DoEvents();
}
catch (Exception ex)
{
}
}
2025-03-07 09:06:46 +08:00
}
}