-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathProgram.cs
More file actions
136 lines (119 loc) · 4.39 KB
/
Program.cs
File metadata and controls
136 lines (119 loc) · 4.39 KB
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
using System;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
using RyzenTuner.Common.Container;
using RyzenTuner.UI;
namespace RyzenTuner
{
internal static class Program
{
[STAThread]
private static void Main()
{
try
{
Application.ThreadException += Application_ThreadException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
if (Environment.OSVersion.Version.Major >= 6)
{
SetProcessDPIAware();
}
AutoSelectLang();
var runningProcesses = Process.GetProcessesByName("RyzenTuner");
try
{
if (runningProcesses.Length > 1)
{
throw new Exception(Properties.Strings.TextExceptionOnlyOneProgramIsAllowedToRun);
}
}
finally
{
foreach (var process in runningProcesses)
{
process.Dispose();
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
AppContainer.Dispose();
}
catch (Exception ex)
{
// 检查是否是初始化失败导致的异常
if (HasException<DllNotFoundException>(ex) ||
ex.Message.Contains("libryzenadj.dll"))
{
ShowErrorAndExit(Properties.Strings.TextCriticalError,
Properties.Strings.TextLibRyzenAdjMissing);
}
else if (HasException<EntryPointNotFoundException>(ex))
{
ShowErrorAndExit(Properties.Strings.TextCriticalError,
Properties.Strings.TextLibRyzenAdjTooOld);
}
else
{
ShowErrorAndExit(Properties.Strings.TextFatalError,
Properties.Strings.TextUnhandledException.Replace("{message}", ex.Message));
}
}
}
// 显示错误并退出的辅助方法
static void ShowErrorAndExit(string title, string message)
{
MessageBox.Show(message, title,
MessageBoxButtons.OK, MessageBoxIcon.Error);
AppContainer.Dispose();
Environment.Exit(1);
}
/// <summary>
/// 根据用户系统语言,自动选择合适的语言
/// </summary>
private static void AutoSelectLang()
{
var currentCulture = Thread.CurrentThread.CurrentCulture;
// 非中文环境全部切换为英文
if (!currentCulture.ToString().StartsWith("zh-"))
{
var culture = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
}
}
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
_handleUnhandledException(e.Exception);
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs arg)
{
var e = (Exception)arg.ExceptionObject;
_handleUnhandledException(e);
}
private static void _handleUnhandledException(Exception ex)
{
MessageBox.Show(ex.Message, Properties.Strings.TextExceptionTitle,
MessageBoxButtons.OK, MessageBoxIcon.Error);
AppContainer.Logger().LogException(ex);
AppContainer.Dispose();
Application.Exit();
}
private static bool HasException<T>(Exception ex) where T : Exception
{
while (ex != null)
{
if (ex is T)
{
return true;
}
ex = ex.InnerException;
}
return false;
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool SetProcessDPIAware();
}
}