-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
155 lines (126 loc) · 5.91 KB
/
Program.cs
File metadata and controls
155 lines (126 loc) · 5.91 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TrexMinerGUI
{
static class Program
{
private static EventHandler IdleEventHandler;
public static MainAppContext TheMainAppContext;
public static StopWatchWrapper TheStopWatchWrapper;
public static TrexWrapper TheTrexWrapper;
public static SelfUpdate TheSelfUpdate;
public static Config TheConfig;
public static string ExecutionPath;
public static string ExceptionLogFileName;
public static Mutex TheMutex;
public static bool ApplicationStarted;
[STAThread]
public static void Main(string[] args)
{
ApplicationStarted = false;
ExecutionPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\";
ExceptionLogFileName = "trex_gui_exception_log.txt";
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionMessageBox);
Application.ThreadException += new ThreadExceptionEventHandler(UIThreadUnhandledExceptionMessageBox);
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
TheMutex = new Mutex(true, "TrexMinerGUI");
if (!TheMutex.WaitOne(TimeSpan.Zero, true))
{
MessageBox.Show(owner: null, text: "An instance of application is already running!", caption: "Error", buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error);
System.Environment.Exit(1);
}
if (args.Length >= 1)
{
if (args[0] == "startup")
{
// Lets make sure that the tray icon is visible after login by restarting the app with significant delay
ProcessStartInfo Info = new ProcessStartInfo();
string ThePath = System.Reflection.Assembly.GetExecutingAssembly().Location.Remove(System.Reflection.Assembly.GetExecutingAssembly().Location.Length - 4) + ".exe";
Info.Arguments = "/C timeout /t 4 /nobreak && start \"\" \"" + ThePath + "\"";
Info.WindowStyle = ProcessWindowStyle.Hidden;
Info.CreateNoWindow = true;
Info.FileName = "cmd.exe";
Process.Start(Info);
System.Environment.Exit(0);
}
}
TheConfig = Config.LoadConfigFile();
TheSelfUpdate = new SelfUpdate();
TheMainAppContext = new MainAppContext();
TheStopWatchWrapper = new StopWatchWrapper();
TheTrexWrapper = new TrexWrapper();
if (TheConfig.StartMiningOnAppStart)
Task.Run(() => Program.TheTrexWrapper.Start());
Application.ApplicationExit += new EventHandler(OnApplicationExit);
TheSelfUpdate.CleanUp();
IdleEventHandler = new EventHandler((sender, eventArgs) =>
{
ApplicationStarted = true;
Application.Idle -= IdleEventHandler;
if (Program.TheConfig.IsConfigHasBeenReset)
{
MessageBox.Show("The configuration has been reset! Please reconfigure your settings.", "Welcome!", MessageBoxButtons.OK, MessageBoxIcon.Information);
Program.TheMainAppContext.ShowMainForm(sender, eventArgs);
}
IdleEventHandler = null;
});
Application.Idle += IdleEventHandler;
Application.Run(TheMainAppContext);
}
public static void ShowExceptionMessage(Exception TheException)
{
if (TheException.InnerException != null)
ShowExceptionMessage(TheException.InnerException);
try
{
MessageBox.Show(TheException.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception GUIException)
{
try
{
MessageBox.Show("Fatal exception: \n\n"
+ GUIException.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
Application.Exit();
}
}
}
private static void UIThreadUnhandledExceptionMessageBox(object sender, ThreadExceptionEventArgs e)
{
File.AppendAllText(ExecutionPath + ExceptionLogFileName,
Environment.NewLine + DateTime.Now.ToString() + Environment.NewLine + e.Exception.ToString() + Environment.NewLine);
ShowExceptionMessage(e.Exception);
Application.Exit();
}
private static void UnhandledExceptionMessageBox(object sender, UnhandledExceptionEventArgs e)
{
File.AppendAllText(ExecutionPath + ExceptionLogFileName,
Environment.NewLine + DateTime.Now.ToString() + Environment.NewLine + ((Exception)e.ExceptionObject).ToString() + Environment.NewLine);
ShowExceptionMessage((Exception)e.ExceptionObject);
Application.Exit();
}
private static void OnApplicationExit(object sender, EventArgs e)
{
TheMainAppContext.trayIcon.Visible = false;
TheStopWatchWrapper.SaveToFile();
if (TheTrexWrapper.IsRunning)
TheTrexWrapper.Stop();
if (Program.TheSelfUpdate.UpdateScript != null)
Process.Start(Program.TheSelfUpdate.UpdateScript);
TheMutex.ReleaseMutex();
}
}
}