Skip to content

Commit ca592d2

Browse files
committed
Code Formatting
1 parent 3d9c434 commit ca592d2

2 files changed

Lines changed: 49 additions & 73 deletions

File tree

Helpers/RegistryMonitor.cs

Lines changed: 46 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class RegistryMonitor : IDisposable
4747
{
4848
#region P/Invoke
4949

50-
[DllImport("advapi32.dll", SetLastError = true)]
50+
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
5151
private static extern int RegOpenKeyEx(IntPtr hKey, string subKey, uint options, int samDesired,
5252
out IntPtr phkResult);
5353

@@ -63,13 +63,13 @@ private static extern int RegNotifyChangeKeyValue(IntPtr hKey, bool bWatchSubtre
6363
private const int KEY_NOTIFY = 0x0010;
6464
private const int STANDARD_RIGHTS_READ = 0x00020000;
6565

66-
private static readonly IntPtr HKEY_CLASSES_ROOT = new IntPtr(unchecked((int) 0x80000000));
67-
private static readonly IntPtr HKEY_CURRENT_USER = new IntPtr(unchecked((int) 0x80000001));
68-
private static readonly IntPtr HKEY_LOCAL_MACHINE = new IntPtr(unchecked((int) 0x80000002));
69-
private static readonly IntPtr HKEY_USERS = new IntPtr(unchecked((int) 0x80000003));
70-
private static readonly IntPtr HKEY_PERFORMANCE_DATA = new IntPtr(unchecked((int) 0x80000004));
71-
private static readonly IntPtr HKEY_CURRENT_CONFIG = new IntPtr(unchecked((int) 0x80000005));
72-
private static readonly IntPtr HKEY_DYN_DATA = new IntPtr(unchecked((int) 0x80000006));
66+
private static readonly IntPtr HKEY_CLASSES_ROOT = new(unchecked((int) 0x80000000));
67+
private static readonly IntPtr HKEY_CURRENT_USER = new(unchecked((int)0x80000001));
68+
private static readonly IntPtr HKEY_LOCAL_MACHINE = new(unchecked((int)0x80000002));
69+
private static readonly IntPtr HKEY_USERS = new(unchecked((int)0x80000003));
70+
private static readonly IntPtr HKEY_PERFORMANCE_DATA = new(unchecked((int)0x80000004));
71+
private static readonly IntPtr HKEY_CURRENT_CONFIG = new(unchecked((int)0x80000005));
72+
private static readonly IntPtr HKEY_DYN_DATA = new(unchecked((int)0x80000006));
7373

7474
#endregion
7575

@@ -78,7 +78,7 @@ private static extern int RegNotifyChangeKeyValue(IntPtr hKey, bool bWatchSubtre
7878
/// <summary>
7979
/// Occurs when the specified registry key has changed.
8080
/// </summary>
81-
public event EventHandler RegChanged;
81+
public event EventHandler? RegChanged;
8282

8383
/// <summary>
8484
/// Raises the <see cref="RegChanged"/> event.
@@ -94,15 +94,13 @@ private static extern int RegNotifyChangeKeyValue(IntPtr hKey, bool bWatchSubtre
9494
/// </remarks>
9595
protected virtual void OnRegChanged()
9696
{
97-
EventHandler handler = RegChanged;
98-
if (handler != null)
99-
handler(this, null);
100-
}
97+
RegChanged?.Invoke(this, null);
98+
}
10199

102100
/// <summary>
103101
/// Occurs when the access to the registry fails.
104102
/// </summary>
105-
public event ErrorEventHandler Error;
103+
public event ErrorEventHandler? Error;
106104

107105
/// <summary>
108106
/// Raises the <see cref="Error"/> event.
@@ -119,21 +117,19 @@ protected virtual void OnRegChanged()
119117
/// </remarks>
120118
protected virtual void OnError(Exception e)
121119
{
122-
ErrorEventHandler handler = Error;
123-
if (handler != null)
124-
handler(this, new ErrorEventArgs(e));
125-
}
120+
Error?.Invoke(this, new ErrorEventArgs(e));
121+
}
126122

127123
#endregion
128124

129125
#region Private member variables
130126

131-
private IntPtr _registryHive;
132-
private string _registrySubName;
133-
private object _threadLock = new object();
134-
private Thread _thread;
127+
private IntPtr? _registryHive;
128+
private string? _registrySubName;
129+
private object _threadLock = new();
130+
private Thread? _thread;
135131
private bool _disposed = false;
136-
private ManualResetEvent _eventTerminate = new ManualResetEvent(false);
132+
private ManualResetEvent _eventTerminate = new(false);
137133

138134
private RegChangeNotifyFilter _regFilter = RegChangeNotifyFilter.Key | RegChangeNotifyFilter.Attribute |
139135
RegChangeNotifyFilter.Value | RegChangeNotifyFilter.Security;
@@ -156,7 +152,7 @@ public RegistryMonitor(RegistryKey registryKey)
156152
public RegistryMonitor(string name)
157153
{
158154
if (name == null || name.Length == 0)
159-
throw new ArgumentNullException("name");
155+
throw new ArgumentNullException(nameof(name));
160156

161157
InitRegistryKey(name);
162158
}
@@ -203,40 +199,17 @@ public RegChangeNotifyFilter RegChangeNotifyFilter
203199

204200
private void InitRegistryKey(RegistryHive hive, string name)
205201
{
206-
switch (hive)
207-
{
208-
case RegistryHive.ClassesRoot:
209-
_registryHive = HKEY_CLASSES_ROOT;
210-
break;
211-
212-
case RegistryHive.CurrentConfig:
213-
_registryHive = HKEY_CURRENT_CONFIG;
214-
break;
215-
216-
case RegistryHive.CurrentUser:
217-
_registryHive = HKEY_CURRENT_USER;
218-
break;
219-
220-
//case RegistryHive.DynData:
221-
// _registryHive = HKEY_DYN_DATA;
222-
// break;
223-
224-
case RegistryHive.LocalMachine:
225-
_registryHive = HKEY_LOCAL_MACHINE;
226-
break;
227-
228-
case RegistryHive.PerformanceData:
229-
_registryHive = HKEY_PERFORMANCE_DATA;
230-
break;
231-
232-
case RegistryHive.Users:
233-
_registryHive = HKEY_USERS;
234-
break;
235-
236-
default:
237-
throw new InvalidEnumArgumentException("hive", (int)hive, typeof (RegistryHive));
238-
}
239-
_registrySubName = name;
202+
_registryHive = hive switch
203+
{
204+
RegistryHive.ClassesRoot => HKEY_CLASSES_ROOT,
205+
RegistryHive.CurrentConfig => HKEY_CURRENT_CONFIG,
206+
RegistryHive.CurrentUser => HKEY_CURRENT_USER,
207+
RegistryHive.LocalMachine => HKEY_LOCAL_MACHINE,
208+
RegistryHive.PerformanceData => HKEY_PERFORMANCE_DATA,
209+
RegistryHive.Users => HKEY_USERS,
210+
_ => throw new InvalidEnumArgumentException("hive", (int)hive, typeof(RegistryHive)),
211+
};
212+
_registrySubName = name;
240213
}
241214

242215
private void InitRegistryKey(string name)
@@ -270,10 +243,10 @@ private void InitRegistryKey(string name)
270243

271244
default:
272245
_registryHive = IntPtr.Zero;
273-
throw new ArgumentException("The registry hive '" + nameParts[0] + "' is not supported", "value");
246+
throw new ArgumentException($"The registry hive '{nameParts[0]}' is not supported", "value");
274247
}
275248

276-
_registrySubName = String.Join("\\", nameParts, 1, nameParts.Length - 1);
249+
_registrySubName = string.Join("\\", nameParts, 1, nameParts.Length - 1);
277250
}
278251

279252
#endregion
@@ -300,9 +273,11 @@ public void Start()
300273
if (!IsMonitoring)
301274
{
302275
_eventTerminate.Reset();
303-
_thread = new Thread(new ThreadStart(MonitorThread));
304-
_thread.IsBackground = true;
305-
_thread.Start();
276+
_thread = new Thread(new ThreadStart(MonitorThread))
277+
{
278+
IsBackground = true
279+
};
280+
_thread.Start();
306281
}
307282
}
308283
}
@@ -317,8 +292,7 @@ public void Stop()
317292

318293
lock (_threadLock)
319294
{
320-
Thread thread = _thread;
321-
if (thread != null)
295+
if (_thread is Thread thread)
322296
{
323297
_eventTerminate.Set();
324298
thread.Join();
@@ -341,15 +315,17 @@ private void MonitorThread()
341315

342316
private void ThreadLoop()
343317
{
344-
IntPtr registryKey;
345-
int result = RegOpenKeyEx(_registryHive, _registrySubName, 0, STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_NOTIFY,
346-
out registryKey);
347-
if (result != 0)
318+
int result = RegOpenKeyEx(_registryHive.Value,
319+
_registrySubName,
320+
0,
321+
STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_NOTIFY,
322+
out IntPtr registryKey);
323+
if (result != 0)
348324
throw new Win32Exception(result);
349325

350326
try
351327
{
352-
AutoResetEvent _eventNotify = new AutoResetEvent(false);
328+
AutoResetEvent _eventNotify = new(false);
353329
WaitHandle[] waitHandles = new WaitHandle[] {_eventNotify, _eventTerminate};
354330
while (!_eventTerminate.WaitOne(0, true))
355331
{

Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public class AppContext : ApplicationContext {
8989
private AboutForm? aboutForm = null;
9090
private bool isLightTheme = false;
9191
private AppSettings? appSettings;
92+
private const string themeKeyPath = "Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize";
9293

9394
[STAThread]
9495
private static void Main() {
@@ -120,8 +121,7 @@ public AppContext() {
120121

121122
SetIsLightTheme();
122123

123-
string keyPath = "Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize";
124-
if (Registry.CurrentUser.OpenSubKey(keyPath) is RegistryKey key) {
124+
if (Registry.CurrentUser.OpenSubKey(themeKeyPath) is RegistryKey key) {
125125
RegistryMonitor monitor = new(key);
126126
monitor.RegChanged += new EventHandler(SetIsLightTheme);
127127
monitor.Start();
@@ -150,7 +150,7 @@ public AppContext() {
150150

151151
private void SetIsLightTheme(object? sender = null, EventArgs? e = null) {
152152
try {
153-
using (RegistryKey? key = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize")) {
153+
using (RegistryKey? key = Registry.CurrentUser.OpenSubKey(themeKeyPath)) {
154154
if (key != null) {
155155
Object? o = key.GetValue("SystemUsesLightTheme");
156156
if (o != null) {

0 commit comments

Comments
 (0)