示例#1
0
        public static void EnableChangeWatching()
        {
            if (watcher != null)
            {
                return;
            }

            watcher = new FileSystemWatcher(Installation.Properties.ConfigurationDirectory, "*.xml");
            watcher.NotifyFilter = NotifyFilters.LastWrite;
            watcher.Changed     += new FileSystemEventHandler(delegate(object sender, FileSystemEventArgs e)
            {
                var serializer = config.Select(s => s.Value).FirstOrDefault(s => s.Filename == Path.GetFileName(e.FullPath));
                if (serializer != null)
                {
                    serializer.Reload();
                    if (Reloaded != null)
                    {
                        Reloaded();
                    }
                }
            });

            // start watching
            watcher.EnableRaisingEvents = true;
        }
        public static void EnableChangeWatching()
        {
            if (watcher != null)
            {
                return;
            }

            watcher = new FileSystemWatcher(Installation.Properties.ConfigurationDirectory, "*.xml");
            watcher.NotifyFilter = NotifyFilters.LastWrite;
            watcher.Changed     += new FileSystemEventHandler(delegate(object sender, FileSystemEventArgs e)
            {
                // This isn't strictly required, but makes sure we only reloaded the configuration once when it has been changed.
                long stamp = File.GetLastWriteTime(e.FullPath).Ticks;
                if (reloadedFiles.ContainsKey(e.Name) && reloadedFiles[e.Name] >= stamp)
                {
                    return;
                }

                reloadedFiles[e.Name] = stamp;
                Task.Factory.StartNew(delegate()
                {
                    var serializer = config.Select(s => s.Value).FirstOrDefault(s => s.Filename == e.Name);
                    if (serializer != null)
                    {
                        Log.Debug("Reloading configuration file '{0}' due to changes.", serializer.Filename);
                        serializer.Reload();
                        if (Reloaded != null)
                        {
                            Reloaded(serializer.ConfigFile);
                        }
                    }
                }).LogOnException();
            });

            // start watching
            watcher.EnableRaisingEvents = true;
        }