-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
77 lines (72 loc) · 2.85 KB
/
Program.cs
File metadata and controls
77 lines (72 loc) · 2.85 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
using LogitechBatteryIndicator.components;
using LogitechBatteryIndicator.controller;
using LogitechBatteryIndicator.helpers;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
namespace LogitechBatteryIndicator
{
internal static partial class Program
{
[LibraryImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool AllocConsole();
static void ExitHandler()
{
DeviceEngine.Instance.Dispose();
TrayIcon.Instance.Dispose();
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
bool startMinimized = false;
if (args.Contains("--console"))
{
AllocConsole();
}
if (args.Contains("--start-minimized"))
{
startMinimized = true;
}
Assembly executingAssembly = Assembly.GetExecutingAssembly();
Process currentProcess = Process.GetCurrentProcess();
var name = executingAssembly.GetName().Name ?? string.Empty;
using (new Mutex(true, "Global\\" + name, out bool createdNew))
{
if (!createdNew)
{
var existingWindowHandle = WindowHelpers.WindowHandle(LogitechBatteryIndicator.Instance.Title);
if (existingWindowHandle != IntPtr.Zero)
{
WindowHelpers.RestoreWindow(existingWindowHandle);
var isInFront = WindowHelpers.BringMainWindowToFront(existingWindowHandle);
return;
}
MessageBox.Show("Another Logitech Battery Indicator process is already running. Please check the system tray and double click the icon there.", "Logitech Battery Indicator");
return;
}
string str = Path.Combine(Path.GetTempPath(), name);
using (new AssemblyLoader(str))
{
ApplicationConfiguration.Initialize();
var form = LogitechBatteryIndicator.Instance;
form.StartMinimized = startMinimized;
form.Load += (sender, e) =>
{
try
{
form.RegisterBatteryUpdateListeners(ref DeviceEngine.Instance.BatteryUpdate);
DeviceEngine.Instance.Init();
}
catch (Exception ex) { Console.Error.WriteLine(ex); }
};
Application.Run(form);
ExitHandler();
}
}
}
}
}