private void ThreadLoop() { // make the RegOpenKeyEx call IntPtr registryKey; int result = RegOpenKeyEx(registryHive, registrySubName, 0, STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_NOTIFY, out registryKey); if(result != 0) { throw new Win32Exception(result); } try { AutoResetEvent eventNotify = new AutoResetEvent(false); // set up the events that we act on WaitHandle[] waitHandles = new WaitHandle[] { eventNotify, eventTerminate }; while(!eventTerminate.WaitOne(0, true)) { // make the RegNotifyChangeKeyValue call to monitor the key result = RegNotifyChangeKeyValue(registryKey, true, regFilter, eventNotify.SafeWaitHandle, true); if(result != 0) { throw new Win32Exception(result); } // the signal we waited on was an eventNotify if(WaitHandle.WaitAny(waitHandles) == 0) { // create a RegistryKey object using the handle we got earlier RegistryKey changedRegKey = RegistryKey.FromHandle(new SafeRegistryHandle(registryKey, true)); // send this RegistryKey object back to the event handler OnRegChangedEventArgs args = new OnRegChangedEventArgs(); // for our purposes, the time the key changed was now args.timeChanged = DateTime.Now; args.changedKey = changedRegKey; // call the handler and signal the event OnRegChanged(args); eventNotify.Set(); } } } finally { // close the key if(registryKey != IntPtr.Zero) { RegCloseKey(registryKey); } } }
/// <summary> /// Event handler for registry changes. Extracts the value of AccentPalette and passes it along /// </summary> /// <param name="e">OnRegChangedEvents object that contains the changed registry key</param> private void onRegistryChange(object sender, OnRegChangedEventArgs e) { // get the value from the RegistryKey object setColor((byte[])e.changedKey.GetValue(accentPalette)); }
/// <summary> /// Raises a <see cref="RegChanged"/> event /// </summary> /// <remarks> /// <para> /// <b>OnRegChanged</b> is called when the registry key changes /// </para> /// </remarks> /// <param name="e">object containing details of the raised event (registry changes)</param> protected virtual void OnRegChanged(OnRegChangedEventArgs e) { EventHandler<OnRegChangedEventArgs> handler = RegChanged; if(handler != null) { handler(this, e); } }