Example #1
0
        private void Dispose(Boolean disposing)
        {
            UnsubscribeFromInternalWatcherEvents();

            if (disposing)
            {
                InternalWatcher.Dispose();
            }
        }
Example #2
0
        /// <summary>
        /// Refreshes the internal FileSystemWatcher
        /// </summary>
        /// <param name="returnWhenRefreshed">In case another thread is alreayd refreshing, determines wether the thread should return before the refreshing thread finishes or not.</param>
        private void Refresh(Boolean returnWhenRefreshed)
        {
            // Making sure another thread isn't already refreshing:
            if (!Monitor.TryEnter(_refreshLock))
            {
                // if another thread IS already refreshing - wait for it to finish then return
                if (returnWhenRefreshed)
                {
                    WaitForRefresh();
                }

                return;
            }

            IsRefreshing = true;

            // 1. unsubscribe from old watcher's events.
            UnsubscribeFromInternalWatcherEvents();

            // 2a. Keeping the current internal "EnableRaisingEvents" value
            Boolean currentEnableRaisingEvents = InternalWatcher.EnableRaisingEvents;

            // 2b. Turning off EnableRaisingEvents to avoid "locking" the watched folder
            InternalWatcher.EnableRaisingEvents = false;

            // 3. Get a new watcher
            IWatcher newInternalWatcher = GetReplacementWatcher();

            newInternalWatcher.EnableRaisingEvents = currentEnableRaisingEvents;

            // 4. Disposing of the old watcher
            InternalWatcher.Dispose();

            // 5. Place new watcher in the Internal watcher property
            //    This also registers to the watcher's events
            InternalWatcher = newInternalWatcher;

            // Change state back to "not refreshing"
            IsRefreshing = false;
            // Notify any waiting threads that the refresh is done
            foreach (ManualResetEventSlim waitingThreadEvent in _waitingThreadsEvents.Values)
            {
                waitingThreadEvent.Set();
            }

            _waitingThreadsEvents.Clear();
            Monitor.Exit(_refreshLock);

            // Notify listeners about the refresh.
            Refreshed?.Invoke(this, new EventArgs());
        }