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);
        }
Ejemplo n.º 2
0
            /// <summary>
            /// Invokes the associated event handler.
            /// </summary>
            /// <returns>True if the watcher will be run again; false if the
            /// entry has been closed and can be removed.</returns>
            public bool Invoke(RegistryWatcher sender)
            {
                if (_key == null)
                {
                    // Returns true so we don't try and remove null entries from
                    // the list.
                    return(true);
                }

                if (!_registered)
                {
                    return(false);
                }

                _callback(sender, _args);

                if (_args.CancelWatcher)
                {
                    Unregister();
                    return(false);
                }

                lock (this) {
                    // only re-register if we haven't been closed
                    if (_registered)
                    {
                        try {
                            Register();
                        } catch (Win32Exception ex) {
                            // If we fail to re-register (probably because the
                            // key has been deleted), there's nothing that can
                            // be done. Fail if we're debugging, otherwise just
                            // continue without registering the watcher again.
                            Debug.Fail("Error registering registry watcher: " + ex.ToString());
                            _registered = false;
                        }
                    }
                }
                return(true);
            }
Ejemplo n.º 3
0
            /// <summary>
            /// Invokes the associated event handler.
            /// </summary>
            /// <returns>True if the watcher will be run again; false if the
            /// entry has been closed and can be removed.</returns>
            public bool Invoke(RegistryWatcher sender) {
                if (_key == null) {
                    // Returns true so we don't try and remove null entries from
                    // the list.
                    return true;
                }

                if (!_registered) {
                    return false;
                }

                _callback(sender, _args);

                if (_args.CancelWatcher) {
                    Unregister();
                    return false;
                }

                lock (this) {
                    // only re-register if we haven't been closed
                    if (_registered) {
                        try {
                            Register();
                        } catch (Win32Exception ex) {
                            // If we fail to re-register (probably because the
                            // key has been deleted), there's nothing that can
                            // be done. Fail if we're debugging, otherwise just
                            // continue without registering the watcher again.
                            Debug.Fail("Error registering registry watcher: " + ex.ToString());
                            _registered = false;
                        }
                    }
                }
                return true;
            }
Ejemplo n.º 4
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;
        }