-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
executable file
·69 lines (61 loc) · 2.47 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
using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;
namespace PiperTray
{
static class Program
{
static Mutex mutex = new Mutex(true, "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}");
[STAThread]
static void Main()
{
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
if (mutex.WaitOne(TimeSpan.Zero, true))
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var app = PiperTrayApp.GetInstance();
app.Initialize();
// Set up logging only if it's enabled in settings
if (PiperTrayApp.IsLoggingEnabled)
{
string logPath = Path.Combine(Application.StartupPath, "system.log");
Console.SetOut(new FileLogger(logPath));
}
Application.Run(app);
}
finally
{
mutex.ReleaseMutex();
}
}
else
{
MessageBox.Show("Another instance of Piper Tray is already running.", "Piper Tray", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
LogUnhandledException(e.Exception, "Thread Exception");
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
LogUnhandledException((Exception)e.ExceptionObject, "Unhandled Exception");
}
static void LogUnhandledException(Exception ex, string source)
{
string logPath = Path.Combine(Application.StartupPath, "crash.log");
using (StreamWriter writer = new StreamWriter(logPath, true))
{
writer.WriteLine($"{DateTime.Now}: {source}");
writer.WriteLine(ex.ToString());
writer.WriteLine();
}
}
}
}