-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
147 lines (130 loc) · 4.42 KB
/
Copy pathProgram.cs
File metadata and controls
147 lines (130 loc) · 4.42 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
namespace SecondaryMonitorStutterFix;
internal static class Program
{
private const string MutexName = @"Local\SecondaryMonitorStutterFix-7D4D37BB";
private const string ActivationEventName = @"Local\SecondaryMonitorStutterFix-Activate-7D4D37BB";
[STAThread]
private static int Main(string[] args)
{
ApplicationConfiguration.Initialize();
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.ThreadException += (_, eventArgs) => AppLog.Write("Unhandled UI-thread exception.", eventArgs.Exception);
AppDomain.CurrentDomain.UnhandledException += (_, eventArgs) =>
AppLog.Write("Unhandled process exception.", eventArgs.ExceptionObject as Exception);
if (args.Contains("--self-test", StringComparer.OrdinalIgnoreCase))
{
return RunSelfTest();
}
if (args.Contains("--startup-self-test", StringComparer.OrdinalIgnoreCase))
{
return RunStartupSelfTest();
}
using var activationEvent = new EventWaitHandle(
initialState: false,
EventResetMode.AutoReset,
ActivationEventName);
using var instanceMutex = new Mutex(initiallyOwned: true, MutexName, out var isFirstInstance);
if (!isFirstInstance)
{
activationEvent.Set();
return 0;
}
var startHidden = args.Contains("--background", StringComparer.OrdinalIgnoreCase);
using var mainForm = new MainForm(startHidden);
_ = mainForm.Handle;
var activationRegistration = ThreadPool.RegisterWaitForSingleObject(
activationEvent,
(_, timedOut) =>
{
if (!timedOut)
{
mainForm.RequestShowSettings();
}
},
null,
Timeout.Infinite,
executeOnlyOnce: false);
try
{
Application.Run(mainForm);
}
finally
{
_ = activationRegistration.Unregister(null);
}
GC.KeepAlive(instanceMutex);
return 0;
}
private static int RunSelfTest()
{
try
{
var monitors = MonitorInfo.Discover();
if (monitors.Count == 0)
{
return 2;
}
if (monitors.Any(monitor => string.IsNullOrWhiteSpace(monitor.PersistentId)) ||
monitors.Select(monitor => monitor.PersistentId).Distinct(StringComparer.OrdinalIgnoreCase).Count() != monitors.Count)
{
return 5;
}
var surfaces = monitors
.SelectMany(monitor => SurfaceMode.Standard.GetSurfaceSpecs()
.Select(spec => new AnimatedSurfaceForm(monitor.Screen, spec)))
.ToArray();
using var scheduling = NativeMethods.EnterSynchronizedScheduling();
try
{
foreach (var surface in surfaces)
{
surface.ShowSurface();
}
for (var frame = 0; frame < 10; frame++)
{
foreach (var surface in surfaces)
{
surface.RenderNextFrame(synchronous: true);
}
NativeMethods.FlushComposition();
Application.DoEvents();
Thread.Sleep(10);
}
}
finally
{
foreach (var surface in surfaces)
{
surface.Close();
surface.Dispose();
}
}
return surfaces.All(surface => surface.FrameCount >= 5) ? 0 : 3;
}
catch
{
return 4;
}
}
private static int RunStartupSelfTest()
{
if (StartupShortcutManager.IsEnabled())
{
return 6;
}
try
{
var enabled = StartupShortcutManager.SetEnabled(true);
if (!enabled.Success || !StartupShortcutManager.IsEnabled())
{
return 7;
}
var disabled = StartupShortcutManager.SetEnabled(false);
return disabled.Success && !StartupShortcutManager.IsEnabled() ? 0 : 8;
}
finally
{
_ = StartupShortcutManager.SetEnabled(false);
}
}
}