You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.8 KiB

using EC.AutoWeightServer.UI;
using EC.Utils;
using EC.Utils.Storage;
using System;
using System.Data.SqlClient;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace EC.AutoWeightServer
{
internal static class Program
{
private static Mutex _mutex;
/// <summary>
/// 应用程序的主入口点
/// </summary>
[STAThread]
private static void Main()
{
EnvUtil.InitWorkDirectory();
EnvUtil.AddDllPath(EnvUtil.CombinePath(EnvUtil.GetWorkDirectory(), "dll"));
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += Application_ThreadException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
// 在应用程序启动时运行的代码
log4net.Config.XmlConfigurator.Configure();
_mutex = new Mutex(false, "AutoWeightServer", out var bCreatedNew);
if (bCreatedNew)
{
LocalStorage.Init();
var frmMainServer = new FrmMainServer { StartPosition = FormStartPosition.CenterScreen };
Application.Run(frmMainServer);
}
else
{
MessageBox.Show("程序已经运行!不能重复运行!");
}
}
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
string str = GetExceptionMsg(e.Exception, e.ToString());
LogUnit.Error("全局异常捕获 CSUI:\r\n" + str);
MessageBox.Show("操作执行异常,可尝试重新执行!\r\n\r\n" + e.Exception.Message, "UIException " + e.Exception.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(1);
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
LogUnit.Error("全局异常捕获 CSUnUI:\r\n" + str);
MessageBox.Show("操作执行异常,程序即将关闭!\r\n\r\n" + (e.ExceptionObject as Exception)?.Message, "UnUIException " + (e.ExceptionObject as Exception)?.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(1);
}
private static string GetExceptionMsg(Exception ex, string backStr)
{
string num = (ex.GetType().Name != "SqlException") ? "" : " " + (ex as SqlException).Number.ToString();
var builder = new StringBuilder();
builder.AppendLine("【出现时间】:" + DateTime.Now);
if (ex != null)
{
builder.AppendLine("【异常类型】:" + ex.GetType().Name + num);
builder.AppendLine("【异常信息】:" + ex.Message);
builder.AppendLine("【堆栈调用】:" + ex.StackTrace);
}
else
{
builder.AppendLine("【未处理异常】:" + backStr);
}
return builder.ToString();
}
}
}