-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
82 lines (74 loc) · 3.11 KB
/
Copy pathProgram.cs
File metadata and controls
82 lines (74 loc) · 3.11 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
78
79
80
81
82
namespace CapsNumTray;
internal static class Program
{
[STAThread]
static void Main(string[] args)
{
#if DEBUG
// Debug-only DPI render harness — realizes each form at the real screen
// DPI and dumps PNGs, then exits. Runs before the single-instance mutex
// (it's a one-shot render, not a tray session) so it works alongside a
// live instance on a test VM. Stripped from Release.
if (args.Contains("--diag-render-form"))
{
DiagRender.Run(args);
return;
}
if (args.Contains("--diag-measure-settings"))
{
DiagRender.MeasureSettings(args);
return;
}
if (args.Contains("--diag-measure-update"))
{
DiagRender.MeasureUpdate(args);
return;
}
#endif
bool isAfterUpdate = args.Contains("--after-update");
// --after-theme-restart: dispatched by TrayApplication when the user
// changes the Theme dropdown. Same mutex-retry treatment as --after-update
// because we're racing the outgoing instance for the single-instance lock.
bool isAfterThemeRestart = args.Contains("--after-theme-restart");
// Single-instance: hold mutex for lifetime. Post-update / post-theme-restart
// the outgoing exe needs a moment to release the mutex, so retry briefly
// in those cases. 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 || isAfterThemeRestart) ? 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();
if (isAfterThemeRestart)
OsdForm.ShowDelayedOsd("Theme applied.", delayMs: 800, dwellMs: 1800);
using var app = new TrayApplication();
Application.Run(app);
}
}