-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
60 lines (51 loc) · 1.8 KB
/
Program.cs
File metadata and controls
60 lines (51 loc) · 1.8 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
using System.Runtime.InteropServices;
namespace BrightnessController;
internal static class Program
{
private const string MutexName = "BrightnessController_SingleInstance_Mutex";
[STAThread]
static void Main()
{
using var mutex = new Mutex(initiallyOwned: true, MutexName,
out bool createdNew);
if (!createdNew)
{
MessageBox.Show(
"Brightness Controller is already running.\n" +
"Look for the sun icon in the system tray (^ arrow).",
"Already Running",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
return;
}
Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += (_, e) =>
HandleUnhandledException(e.Exception);
AppDomain.CurrentDomain.UnhandledException += (_, e) =>
HandleUnhandledException(e.ExceptionObject as Exception);
Application.Run(new TrayApplicationContext());
// Keep mutex alive for entire session
mutex.ReleaseMutex();
}
private static void HandleUnhandledException(Exception? ex)
{
if (ex == null) return;
#if DEBUG
MessageBox.Show(ex.ToString(), "Unhandled Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
#else
try
{
string dir = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"BrightnessController");
Directory.CreateDirectory(dir);
File.AppendAllText(Path.Combine(dir, "error.log"),
$"[{DateTime.Now:u}] {ex}\n\n");
}
catch { }
#endif
}
}