Some days ago I read an article at The Old New Thing about performance cost of reading a registry key. As I am reading registry keys in quite frequently I wonderd how to avoid polling registry keys in .NET (C#). Windows API provides a function RegNotifyChangeKeyValue which notifies the caller about changes. Sadly, this function is not provided by the Microsoft.Win32.RegistryKey. So I tried writing a wrapper class for this.
I don't claim that this is perfect, so any comment would be appreciated !
Downloads:
RegistryKeyMonitor 0.1 (source and binaries)
Usage:
-
using System;
-
using System.Threading;
-
using Microsoft.Win32;
-
-
namespace RegistryKeyMonitorTester
-
{
-
class Tester
-
{
-
[STAThread]
-
public static void Main() {
-
monitor.Start();
-
LongRunningTask();
-
monitor.Stop();
-
}
-
-
private static void LongRunningTask() {
-
for(int i=0; i<60; ++i) {
-
Console.WriteLine("Try changing some registry keys");
-
Thread.Sleep(1000);
-
}
-
}
-
-
private static void monitor_RegKeyChanged(object sender, EventArgs e) {
-
RegistryKeyMonitor.RegKeyChangedEventArgs ea = (RegistryKeyMonitor.RegKeyChangedEventArgs) e;
-
Console.WriteLine("RegistryKey " + ea.RegHive.ToString() + "" + ea.RegSubKey + " changed.");
-
}
-
-
private static void monitor_Error(object sender, System.IO.ErrorEventArgs e) {
-
Console.WriteLine(e.GetException().ToString());
-
}
-
}
-
}