forked from openhardwaremonitor/openhardwaremonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
123 lines (105 loc) · 3.77 KB
/
Program.cs
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
/*
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
Copyright (C) 2009-2020 Michael Möller <[email protected]>
*/
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using OpenHardwareMonitor.GUI;
namespace OpenHardwareMonitor {
public static class Program {
[STAThread]
public static void Main() {
#if !DEBUG
Application.ThreadException +=
new ThreadExceptionEventHandler(Application_ThreadException);
Application.SetUnhandledExceptionMode(
UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
#endif
if (!AllRequiredFilesAvailable() || !IsNetFramework45Installed())
Environment.Exit(0);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using (GUI.MainForm form = new GUI.MainForm()) {
form.FormClosed += delegate(Object sender, FormClosedEventArgs e) {
Application.Exit();
};
Application.Run();
}
}
private static bool IsFileAvailable(string fileName) {
string path = Path.GetDirectoryName(Application.ExecutablePath) +
Path.DirectorySeparatorChar;
if (!File.Exists(path + fileName)) {
MessageBox.Show("The following file could not be found: " + fileName +
"\nPlease extract all files from the archive.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}
private static bool AllRequiredFilesAvailable() {
if (!IsFileAvailable("Aga.Controls.dll"))
return false;
if (!IsFileAvailable("OpenHardwareMonitorLib.dll"))
return false;
if (!IsFileAvailable("OxyPlot.dll"))
return false;
if (!IsFileAvailable("OxyPlot.WindowsForms.dll"))
return false;
return true;
}
private static bool IsNetFramework45Installed() {
Type type;
try {
type = TryGetDefaultDllImportSearchPathsAttributeType();
} catch (TypeLoadException) {
MessageBox.Show(
"This application requires the .NET Framework 4.5 or a later version.\n" +
"Please install the latest .NET Framework. For more information, see\n\n" +
"https://dotnet.microsoft.com/download/dotnet-framework",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return type != null;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static Type TryGetDefaultDllImportSearchPathsAttributeType() {
return typeof(DefaultDllImportSearchPathsAttribute);
}
private static void ReportException(Exception e) {
CrashForm form = new CrashForm();
form.Exception = e;
form.ShowDialog();
}
public static void Application_ThreadException(object sender,
ThreadExceptionEventArgs e)
{
try {
ReportException(e.Exception);
} catch {
} finally {
Application.Exit();
}
}
public static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs args)
{
try {
Exception e = args.ExceptionObject as Exception;
if (e != null)
ReportException(e);
} catch {
} finally {
Environment.Exit(0);
}
}
}
}