-
Notifications
You must be signed in to change notification settings - Fork 351
/
Copy pathProgram.cs
132 lines (117 loc) · 3.69 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
124
125
126
127
128
129
130
131
132
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using System.IO;
namespace gInk
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
using(Mutex mutex = new Mutex(false, "Global\\" + appGuid))
{
if(!mutex.WaitOne(0, false))
{
return;
}
Application.ThreadException += new ThreadExceptionEventHandler(UIThreadException);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
new Root();
Application.Run();
}
}
private static string appGuid = "86280230-c3d2-4da0-8621-e2a2466fc136";
private static void UIThreadException(object sender, ThreadExceptionEventArgs t)
{
DialogResult result = DialogResult.Cancel;
try
{
Exception ex = (Exception)t.Exception;
string errorMsg = "UIThreadException\r\n\r\n";
errorMsg += "Oops, gInk crashed! Please include the following information if you plan to contact the developers (a copy of the following information is stored in crash.txt in the application folder):\r\n\r\n";
errorMsg += ex.Message + "\r\n\r\n";
errorMsg += "Stack Trace:\r\n" + ex.StackTrace + "\r\n\r\n";
WriteErrorLog(errorMsg);
errorMsg += "!!! PLEASE PRESS ESC KEY TO EXIT IF YOU FEEL YOUR MOUSE CLICK IS BLOCKED BY SOMETHING";
ShowErrorDialog("UIThreadException", errorMsg);
}
catch
{
try
{
MessageBox.Show("Fatal Windows Forms Error", "Fatal Windows Forms Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
}
finally
{
Application.Exit();
}
}
// Exits the program when the user clicks Abort.
if (result == DialogResult.Abort)
Application.Exit();
}
private static void UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
Exception ex = (Exception)e.ExceptionObject;
string errorMsg = "UnhandledException\r\n\r\n";
errorMsg += "Oops, gInk crashed! Please include the following information if you plan to contact the developers:\r\n\r\n";
errorMsg += ex.Message + "\r\n\r\n";
errorMsg += "Stack Trace:\r\n" + ex.StackTrace + "\r\n\r\n";
WriteErrorLog(errorMsg);
ShowErrorDialog("UnhandledException", errorMsg);
if (!EventLog.SourceExists("UnhandledException"))
{
EventLog.CreateEventSource("UnhandledException", "Application");
}
EventLog myLog = new EventLog();
myLog.Source = "UnhandledException";
myLog.WriteEntry(errorMsg);
}
catch (Exception exc)
{
try
{
MessageBox.Show("Fatal Non-UI Error", "Fatal Non-UI Error. Could not write the error to the event log. Reason: " + exc.Message, MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
finally
{
Application.Exit();
}
}
}
private static DialogResult ShowErrorDialog(string title, string errormsg)
{
return MessageBox.Show(errormsg, title, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
}
private static void WriteErrorLog(string errormsg)
{
try
{
FileStream fs = new FileStream("crash.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(errormsg);
sw.Close();
fs.Close();
}
catch
{
FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "crash.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(errormsg);
sw.Close();
fs.Close();
}
}
}
}