Ejemplo n.º 1
0
        /// <summary>
        /// Starts listening for notifications in the specified registry key.
        ///
        /// Each part of the key must be provided separately so that the watcher
        /// can open its own handle.
        /// </summary>
        /// <param name="hive">The hive to watch</param>
        /// <param name="view">The view to watch</param>
        /// <param name="key">The key to watch</param>
        /// <param name="handler">The event handler to invoke</param>
        /// <param name="recursive">True to watch all subkeys as well</param>
        /// <param name="notifyValueChange">
        /// True to notify if a value is added, removed or updated.
        /// </param>
        /// <param name="notifyKeyChange">
        /// True to notify if a subkey is added or removed.
        /// </param>
        /// <param name="tag">
        /// An arbitrary identifier to include with any raised events.
        /// </param>
        /// <returns>An opaque token that can be pased to Remove.</returns>
        public object Add(RegistryHive hive,
                          RegistryView view,
                          string key,
                          RegistryChangedEventHandler handler,
                          bool recursive         = false,
                          bool notifyValueChange = true,
                          bool notifyKeyChange   = true,
                          object tag             = null)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }
            if (!(notifyValueChange | notifyKeyChange))
            {
                throw new InvalidOperationException("Must wait for at least one type of change");
            }

            var args = new RegistryChangedEventArgs(
                hive,
                view,
                key,
                recursive,
                notifyValueChange,
                notifyKeyChange,
                tag
                );

            int             currentWatcher = -1;
            RegistryWatcher watcher;
            var             token = AddInternal(handler, args);

            while (token == null)
            {
                if (_extraWatchers == null)
                {
                    _extraWatchers = new List <RegistryWatcher>();
                }
                currentWatcher += 1;
                if (currentWatcher >= _extraWatchers.Count)
                {
                    watcher = new RegistryWatcher();
                    _extraWatchers.Add(watcher);
                }
                else
                {
                    watcher = _extraWatchers[currentWatcher];
                }
                token = watcher.AddInternal(handler, args);
            }
            return(token);
        }
 private void Registry_Software_Changed(object sender, RegistryChangedEventArgs e) {
     using (var root = RegistryKey.OpenBaseKey(e.Hive, e.View))
     using (var key = root.OpenSubKey(CanopyCorePath)) {
         if (key != null) {
             Registry_Changed(sender, e);
             e.CancelWatcher = true;
             RegistryWatcher.Instance.Add(e.Hive, e.View, CanopyCorePath, Registry_Changed,
                 recursive: true, notifyValueChange: true, notifyKeyChange: true);
         }
     }
 }
 private void Registry_Software_Changed(object sender, RegistryChangedEventArgs e)
 {
     using (var root = RegistryKey.OpenBaseKey(e.Hive, e.View))
         using (var key = root.OpenSubKey(CanopyCorePath)) {
             if (key != null)
             {
                 Registry_Changed(sender, e);
                 e.CancelWatcher = true;
                 RegistryWatcher.Instance.Add(e.Hive, e.View, CanopyCorePath, Registry_Changed,
                                              recursive: true, notifyValueChange: true, notifyKeyChange: true);
             }
         }
 }
Ejemplo n.º 4
0
 public WatchEntry(RegistryChangedEventHandler callback, RegistryChangedEventArgs args)
 {
     using (var baseKey = RegistryKey.OpenBaseKey(args.Hive, args.View)) {
         _key = baseKey.OpenSubKey(args.Key, RegistryKeyPermissionCheck.Default, RegistryRights.Notify);
     }
     if (_key == null)
     {
         throw new ArgumentException("Key does not exist");
     }
     _eventHandle = new AutoResetEvent(false);
     _callback    = callback;
     _args        = args;
     Register();
 }
Ejemplo n.º 5
0
        private object AddInternal(RegistryChangedEventHandler handler, RegistryChangedEventArgs args)
        {
            WatchEntry newEntry;

            lock (_eventsLock) {
                if (_entries.Count >= NativeMethods.MAXIMUM_WAIT_OBJECTS)
                {
                    return(null);
                }
                newEntry = new WatchEntry(handler, args);
                _entries.Add(newEntry);
            }

            _itemAdded.Set();

            return(newEntry);
        }
 private void Registry_Changed(object sender, RegistryChangedEventArgs e) {
     DiscoverInterpreterFactories();
 }
Ejemplo n.º 7
0
 public WatchEntry(RegistryChangedEventHandler callback, RegistryChangedEventArgs args) {
     using (var baseKey = RegistryKey.OpenBaseKey(args.Hive, args.View)) {
         _key = baseKey.OpenSubKey(args.Key, RegistryKeyPermissionCheck.Default, RegistryRights.Notify);
     }
     if (_key == null) {
         throw new ArgumentException("Key does not exist");
     }
     _eventHandle = new AutoResetEvent(false);
     _callback = callback;
     _args = args;
     Register();
 }
Ejemplo n.º 8
0
        private object AddInternal(RegistryChangedEventHandler handler, RegistryChangedEventArgs args) {
            WatchEntry newEntry;

            lock (_eventsLock) {
                if (_entries.Count >= NativeMethods.MAXIMUM_WAIT_OBJECTS) {
                    return null;
                }
                newEntry = new WatchEntry(handler, args);
                _entries.Add(newEntry);
            }

            _itemAdded.Set();

            return newEntry;
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Starts listening for notifications in the specified registry key.
        /// 
        /// Each part of the key must be provided separately so that the watcher
        /// can open its own handle.
        /// </summary>
        /// <param name="hive">The hive to watch</param>
        /// <param name="view">The view to watch</param>
        /// <param name="key">The key to watch</param>
        /// <param name="handler">The event handler to invoke</param>
        /// <param name="recursive">True to watch all subkeys as well</param>
        /// <param name="notifyValueChange">
        /// True to notify if a value is added, removed or updated.
        /// </param>
        /// <param name="notifyKeyChange">
        /// True to notify if a subkey is added or removed.
        /// </param>
        /// <param name="tag">
        /// An arbitrary identifier to include with any raised events.
        /// </param>
        /// <returns>An opaque token that can be pased to Remove.</returns>
        public object Add(RegistryHive hive,
                          RegistryView view,
                          string key,
                          RegistryChangedEventHandler handler,
                          bool recursive = false,
                          bool notifyValueChange = true,
                          bool notifyKeyChange = true,
                          object tag = null) {
            if (key == null) {
                throw new ArgumentNullException("key");
            }
            if (handler == null) {
                throw new ArgumentNullException("handler");
            }
            if (!(notifyValueChange | notifyKeyChange)) {
                throw new InvalidOperationException("Must wait for at least one type of change");
            }

            var args = new RegistryChangedEventArgs(
                hive,
                view,
                key,
                recursive,
                notifyValueChange,
                notifyKeyChange,
                tag
            );

            int currentWatcher = -1;
            RegistryWatcher watcher;
            var token = AddInternal(handler, args);
            while (token == null) {
                if (_extraWatchers == null) {
                    _extraWatchers = new List<RegistryWatcher>();
                }
                currentWatcher += 1;
                if (currentWatcher >= _extraWatchers.Count) {
                    watcher = new RegistryWatcher();
                    _extraWatchers.Add(watcher);
                } else {
                    watcher = _extraWatchers[currentWatcher];
                }
                token = watcher.AddInternal(handler, args);
            }
            return token;
        }
 private void Registry_Changed(object sender, RegistryChangedEventArgs e)
 {
     DiscoverInterpreterFactories();
 }