Blame view

App.xaml.cs 5.5 KB
唐召明 authored
1
2
using FreeSql;
using HHECS.RobotTool.Common;
唐召明 authored
3
using HHECS.RobotTool.Model;
唐召明 authored
4
5
using HHECS.RobotTool.Service;
using HHECS.RobotTool.ViewModel;
唐召明 authored
6
using HHECS.RobotTool.ViewModel.AccountVM;
唐召明 authored
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using HHECS.RobotTool.ViewModel.CommunicationVM;
using HHECS.RobotTool.ViewModel.EquipmentPropVM;
using HHECS.RobotTool.ViewModel.EquipmentVM;
using HHECS.RobotTool.ViewModel.RobotConfigVM;
using Microsoft.Extensions.DependencyInjection;
using NLog;
using System.Configuration;
using System.Reflection;
using System.Windows;
using System.Windows.Threading;
using MessageBox = HandyControl.Controls.MessageBox;

namespace RobotTool
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
唐召明 authored
26
27
        public static User User { get; private set; } = null!;
唐召明 authored
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
        private static Mutex mutex = null!;

        private readonly Logger _log = LogManager.GetCurrentClassLogger();

        public App()
        {
            Services = ConfigureServices();
            InitializeComponent();
        }

        /// <summary>
        /// Gets the current <see cref="App"/> instance in use
        /// </summary>
        public new static App Current => (App)Application.Current;

        /// <summary>
        /// Gets the <see cref="IServiceProvider"/> instance to resolve application services.
        /// </summary>
        public IServiceProvider Services { get; }

        /// <summary>
        /// Configures the services for the application.
        /// </summary>
        private static IServiceProvider ConfigureServices()
        {
            var services = new ServiceCollection();
            var connectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
            IFreeSql fsql = new FreeSqlBuilder()
唐召明 authored
56
57
                //.UseConnectionString(DataType.Sqlite, $"{connectionString}Password=123qwe;")
                .UseConnectionString(DataType.Sqlite, $"{connectionString}")
唐召明 authored
58
59
60
61
62
63
                //.UseMonitorCommand(cmd => Console.WriteLine($"Sql:{cmd.CommandText}"))//监听SQL语句
                .UseAutoSyncStructure(true) //自动同步实体结构到数据库,FreeSql不会扫描程序集,只有CRUD时才会生成表。
                .Build();

            services.AddSingleton(fsql);
唐召明 authored
64
            services.AddHttpClient<HttpService>();
唐召明 authored
65
66
            services.AddSingleton<CenterService>();
            services.AddSingleton<GrooveService>();
唐召明 authored
67
            services.AddSingleton<TcpListenerService>();
唐召明 authored
68
69
70
71
72

            // DataAnalysis
            //services.AddScoped<IAnalysis, FanucAnalysis>();

            // Viewmodels
唐召明 authored
73
74
75
76
77
78
            services.AddTransient<LoginVM>();
            services.AddTransient<RoleVM>();
            services.AddTransient<RoleAddOrEditVM>();
            services.AddTransient<UserVM>();
            services.AddTransient<UserAddOrEditVM>();
唐召明 authored
79
80
81
82
83
84
85
86
87
88
            services.AddTransient<MainVM>();
            services.AddTransient<EquipmentVM>();
            services.AddTransient<EquipmentAddOrEditVM>();
            services.AddTransient<EquipmentPropMonitorVM>();
            services.AddTransient<RobotConfigVM>();
            services.AddTransient<RobotConfigAddOrEditVM>();

            services.AddTransient<CommunicationVM>();
            services.AddTransient<CommunicationAddOrEditVM>();
            services.AddTransient<GrooveComputerVM>();
89
            services.AddTransient<WeldMonitorVM>();
唐召明 authored
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

            return services.BuildServiceProvider();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            //UI线程未捕获异常处理事件(UI主线程)
            DispatcherUnhandledException += App_DispatcherUnhandledException;
            //UI线程未捕获异常处理事件(例如自己创建的一个子线程)
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            //Task线程内未捕获异常处理事件
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException!;

            var assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
            mutex = new Mutex(false, assemblyName);

            if (!mutex.WaitOne(TimeSpan.Zero, true))
            {
                MessageBox.Warning("应用程序已在运行!");
                Shutdown();
                return;
            }

            Application.Current.Exit += (s, args) =>
            {
                mutex.ReleaseMutex();
                mutex.Close();
            };
        }

        private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            Exception ex = e.Exception;
            //MessageBox.Error("Task线程异常", ex.ToString());
            _log.Debug($"UI线程异常{ex}");
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (e.ExceptionObject is Exception ex)
            {
                _log.Debug($"非UI线程异常{ex}", LogType.Debug);
            }
        }

        private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            Exception ex = e.Exception;
            string msg = string.Format("{0}\n\n{1}", ex.Message, ex.StackTrace);//异常信息  调用堆栈信息
            MessageBox.Error("UI线程异常", msg);
            _log.Debug($"UI线程异常{ex}");
            e.Handled = true;//表示异常已处理,可以继续运行
        }
唐召明 authored
143
144

        public static void SetCurrentUser(User user) => User = user;
唐召明 authored
145
146
    }
}