-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
54 lines (47 loc) · 1.93 KB
/
Program.cs
File metadata and controls
54 lines (47 loc) · 1.93 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
namespace CapsNumTray;
internal static class Program
{
[STAThread]
static void Main(string[] args)
{
bool isAfterUpdate = args.Contains("--after-update");
// Single-instance: hold mutex for lifetime. Post-update the outgoing exe
// needs a moment to release the mutex, so retry briefly in that case.
// Local\ scope — the tray is inherently per-session (icons live in the
// session's explorer.exe), so Global\ would let user A block user B on
// multi-session machines (RDS, fast user switching).
Mutex mutex;
bool createdNew;
int remainingRetries = isAfterUpdate ? 50 : 0;
while (true)
{
try
{
mutex = new Mutex(true, @"Local\CapsNumTray_SingleInstance", out createdNew);
}
catch (Exception ex) when (
ex is UnauthorizedAccessException // restricted DACL / Session 0 / AppContainer
or WaitHandleCannotBeOpenedException // name collision with a different kernel object
or IOException) // generic Win32 error leak from the ctor
{
// Any of these mean we cannot claim the single-instance lock.
// Declining is safer than crashing with an unhandled exception.
return;
}
if (createdNew || remainingRetries-- <= 0) break;
mutex.Dispose();
Thread.Sleep(100);
}
using var _mutex = mutex;
if (!createdNew)
return;
UpdateDialog.CleanupUpdateArtifacts();
// Self-heal startup shortcut if exe path has changed (e.g., winget upgrade)
StartupManager.ValidateStartupPath();
ApplicationConfiguration.Initialize();
if (isAfterUpdate)
UpdateDialog.ShowUpdateToast();
using var app = new TrayApplication();
Application.Run(app);
}
}