/// <summary>A synchronous method that returns a structure that contains specific information on the change that occurred, given the type of change you want to monitor and the time (in milliseconds) to wait before timing out.</summary>
        /// <returns>A <see cref="T:System.IO.WaitForChangedResult" /> that contains specific information on the change that occurred.</returns>
        /// <param name="changeType">The <see cref="T:System.IO.WatcherChangeTypes" /> to watch for. </param>
        /// <param name="timeout">The time (in milliseconds) to wait before timing out. </param>
        /// <filterpriority>2</filterpriority>
        public WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType, int timeout)
        {
            WaitForChangedResult result = default(WaitForChangedResult);
            bool flag = EnableRaisingEvents;

            if (!flag)
            {
                EnableRaisingEvents = true;
            }
            bool flag2;

            lock (this)
            {
                waiting = true;
                flag2   = Monitor.Wait(this, timeout);
                if (flag2)
                {
                    result = lastData;
                }
            }
            EnableRaisingEvents = flag;
            if (!flag2)
            {
                result.TimedOut = true;
            }
            return(result);
        }
Exemple #2
0
        public WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType, int timeout)
        {
            WaitForChangedResult result = new WaitForChangedResult();
            bool prevEnabled            = EnableRaisingEvents;

            if (!prevEnabled)
            {
                EnableRaisingEvents = true;
            }

            bool gotData;

            lock (this) {
                waiting = true;
                gotData = Monitor.Wait(this, timeout);
                if (gotData)
                {
                    result = this.lastData;
                }
            }

            EnableRaisingEvents = prevEnabled;
            if (!gotData)
            {
                result.TimedOut = true;
            }

            return(result);
        }
Exemple #3
0
        internal void DispatchEvents(FileAction act, string filename, ref RenamedEventArgs renamed)
        {
            if (disposed)
            {
                return;
            }
            if (waiting)
            {
                lastData = new WaitForChangedResult();
            }

            switch (act)
            {
            case FileAction.Added:
                lastData.Name       = filename;
                lastData.ChangeType = WatcherChangeTypes.Created;
                Task.Run(() => OnCreated(new FileSystemEventArgs(WatcherChangeTypes.Created, path, filename)));
                break;

            case FileAction.Removed:
                lastData.Name       = filename;
                lastData.ChangeType = WatcherChangeTypes.Deleted;
                Task.Run(() => OnDeleted(new FileSystemEventArgs(WatcherChangeTypes.Deleted, path, filename)));
                break;

            case FileAction.Modified:
                lastData.Name       = filename;
                lastData.ChangeType = WatcherChangeTypes.Changed;
                Task.Run(() => OnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, path, filename)));
                break;

            case FileAction.RenamedOldName:
                if (renamed != null)
                {
                    OnRenamed(renamed);
                }
                lastData.OldName    = filename;
                lastData.ChangeType = WatcherChangeTypes.Renamed;
                renamed             = new RenamedEventArgs(WatcherChangeTypes.Renamed, path, filename, "");
                break;

            case FileAction.RenamedNewName:
                lastData.Name       = filename;
                lastData.ChangeType = WatcherChangeTypes.Renamed;
                if (renamed == null)
                {
                    renamed = new RenamedEventArgs(WatcherChangeTypes.Renamed, path, "", filename);
                }
                var renamed_ref = renamed;
                Task.Run(() => OnRenamed(renamed_ref));
                renamed = null;
                break;

            default:
                break;
            }
        }
 /// <devdoc>
 ///     Internal method used for synchronous notification.
 /// </devdoc>
 /// <internalonly/>
 private void OnInternalFileSystemEventArgs(object sender, FileSystemEventArgs e)
 {
     lock (this) {
         // Only change the state of the changed result if it doesn't contain a previous one.
         if (isChanged != true)
         {
             changedResult = new WaitForChangedResult(e.ChangeType, e.Name, false);
             isChanged     = true;
             System.Threading.Monitor.Pulse(this);
         }
     }
 }
Exemple #5
0
 private void OnInternalRenameEventArgs(object sender, RenamedEventArgs e)
 {
     lock (this)
     {
         if (!this.isChanged)
         {
             this.changedResult = new WaitForChangedResult(e.ChangeType, e.Name, e.OldName, false);
             this.isChanged     = true;
             System.Threading.Monitor.Pulse(this);
         }
     }
 }
        internal void DispatchEvents(FileAction act, string filename, ref RenamedEventArgs renamed)
        {
            if (waiting)
            {
                lastData = default(WaitForChangedResult);
            }
            switch (act)
            {
            case FileAction.Added:
                lastData.Name       = filename;
                lastData.ChangeType = WatcherChangeTypes.Created;
                OnCreated(new FileSystemEventArgs(WatcherChangeTypes.Created, path, filename));
                break;

            case FileAction.Removed:
                lastData.Name       = filename;
                lastData.ChangeType = WatcherChangeTypes.Deleted;
                OnDeleted(new FileSystemEventArgs(WatcherChangeTypes.Deleted, path, filename));
                break;

            case FileAction.Modified:
                lastData.Name       = filename;
                lastData.ChangeType = WatcherChangeTypes.Changed;
                OnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, path, filename));
                break;

            case FileAction.RenamedOldName:
                if (renamed != null)
                {
                    OnRenamed(renamed);
                }
                lastData.OldName    = filename;
                lastData.ChangeType = WatcherChangeTypes.Renamed;
                renamed             = new RenamedEventArgs(WatcherChangeTypes.Renamed, path, filename, string.Empty);
                break;

            case FileAction.RenamedNewName:
                lastData.Name       = filename;
                lastData.ChangeType = WatcherChangeTypes.Renamed;
                if (renamed == null)
                {
                    renamed = new RenamedEventArgs(WatcherChangeTypes.Renamed, path, string.Empty, filename);
                }
                OnRenamed(renamed);
                renamed = null;
                break;
            }
        }
Exemple #7
0
        WaitForChangedResult waitForChangedHandler(string directoryPath, string filter,
                                                   WatcherChangeTypes watcherChangeTypes, int timeOut) {
            if (watcherChangeTypes != WatcherChangeTypes.Created)
                throw new NotImplementedException(watcherChangeTypes.ToString());
            var filesList = new ArrayList(Directory.GetFiles(directoryPath, filter));

            var waitForChangedResult = new WaitForChangedResult();
            while (true) {
                var newFilesList = new ArrayList(Directory.GetFiles(directoryPath, filter));
                foreach (string file in newFilesList) {
                    if (!filesList.Contains(file)) {
                        waitForChangedResult.ChangeType = WatcherChangeTypes.Created;
                        waitForChangedResult.Name = file;
                        return waitForChangedResult;

                    }
                }
            }
        }
Exemple #8
0
 static WaitForChangedResult()
 {
     TimedOutResult = new WaitForChangedResult(0, null, true);
 }
		internal void DispatchEvents (FileAction act, string filename, ref RenamedEventArgs renamed)
		{
			if (waiting) {
				lastData = new WaitForChangedResult ();
			}

			switch (act) {
			case FileAction.Added:
				lastData.Name = filename;
				lastData.ChangeType = WatcherChangeTypes.Created;
				OnCreated (new FileSystemEventArgs (WatcherChangeTypes.Created, path, filename));
				break;
			case FileAction.Removed:
				lastData.Name = filename;
				lastData.ChangeType = WatcherChangeTypes.Deleted;
				OnDeleted (new FileSystemEventArgs (WatcherChangeTypes.Deleted, path, filename));
				break;
			case FileAction.Modified:
				lastData.Name = filename;
				lastData.ChangeType = WatcherChangeTypes.Changed;
				OnChanged (new FileSystemEventArgs (WatcherChangeTypes.Changed, path, filename));
				break;
			case FileAction.RenamedOldName:
				if (renamed != null) {
					OnRenamed (renamed);
				}
				lastData.OldName = filename;
				lastData.ChangeType = WatcherChangeTypes.Renamed;
				renamed = new RenamedEventArgs (WatcherChangeTypes.Renamed, path, filename, "");
				break;
			case FileAction.RenamedNewName:
				lastData.Name = filename;
				lastData.ChangeType = WatcherChangeTypes.Renamed;
				if (renamed == null) {
					renamed = new RenamedEventArgs (WatcherChangeTypes.Renamed, path, "", filename);
				}
				OnRenamed (renamed);
				renamed = null;
				break;
			default:
				break;
			}
		}
		public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType, int timeout)
		{
			WaitForChangedResult result = new WaitForChangedResult ();
			bool prevEnabled = EnableRaisingEvents;
			if (!prevEnabled)
				EnableRaisingEvents = true;

			bool gotData;
			lock (this) {
				waiting = true;
				gotData = Monitor.Wait (this, timeout);
				if (gotData)
					result = this.lastData;
			}

			EnableRaisingEvents = prevEnabled;
			if (!gotData)
				result.TimedOut = true;

			return result;
		}
        /// <devdoc>
        ///    <para>
        ///       A synchronous
        ///       method that returns a structure that contains specific information on the change that occurred, given the
        ///       type of change that you wish to monitor and the time (in milliseconds) to wait before timing out.
        ///    </para>
        /// </devdoc>
        public WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType, int timeout)
        {
            FileSystemEventHandler dirHandler    = new FileSystemEventHandler(this.OnInternalFileSystemEventArgs);
            RenamedEventHandler    renameHandler = new RenamedEventHandler(this.OnInternalRenameEventArgs);

            this.isChanged     = false;
            this.changedResult = WaitForChangedResult.TimedOutResult;

            // Register the internal event handler from the given change types.
            if ((changeType & WatcherChangeTypes.Created) != 0)
            {
                this.Created += dirHandler;
            }
            if ((changeType & WatcherChangeTypes.Deleted) != 0)
            {
                this.Deleted += dirHandler;
            }
            if ((changeType & WatcherChangeTypes.Changed) != 0)
            {
                this.Changed += dirHandler;
            }
            if ((changeType & WatcherChangeTypes.Renamed) != 0)
            {
                this.Renamed += renameHandler;
            }

            // Save the Enabled state of this component to revert back to it later (if needed).
            bool savedEnabled = EnableRaisingEvents;

            if (savedEnabled == false)
            {
                runOnce             = true;
                EnableRaisingEvents = true;
            }

            // For each thread entering this wait loop, addref it and wait.  When the last one
            // exits, reset the waiterObject.
            WaitForChangedResult retVal = WaitForChangedResult.TimedOutResult;

            lock (this) {
                if (timeout == -1)
                {
                    while (!isChanged)
                    {
                        System.Threading.Monitor.Wait(this);
                    }
                }
                else
                {
                    System.Threading.Monitor.Wait(this, timeout, true);
                }

                retVal = changedResult;
            }

            // Revert the Enabled flag to its previous state.
            EnableRaisingEvents = savedEnabled;
            runOnce             = false;

            // Decouple the event handlers added above.
            if ((changeType & WatcherChangeTypes.Created) != 0)
            {
                this.Created -= dirHandler;
            }
            if ((changeType & WatcherChangeTypes.Deleted) != 0)
            {
                this.Deleted -= dirHandler;
            }
            if ((changeType & WatcherChangeTypes.Changed) != 0)
            {
                this.Changed -= dirHandler;
            }
            if ((changeType & WatcherChangeTypes.Renamed) != 0)
            {
                this.Renamed -= renameHandler;
            }

            // Return the struct.
            return(retVal);
        }
 /// <devdoc>
 ///     Internal method used for synchronous notification.
 /// </devdoc>
 /// <internalonly/>
 private void OnInternalRenameEventArgs(object sender, RenamedEventArgs e) {
     lock (this) {
         // Only change the state of the changed result if it doesn't contain a previous one.
         if (isChanged != true) {
             changedResult = new WaitForChangedResult(e.ChangeType, e.Name, e.OldName, false);
             isChanged = true;
             System.Threading.Monitor.Pulse(this);
         }
     }
 }
        /// <devdoc>
        ///    <para>
        ///       A synchronous
        ///       method that returns a structure that contains specific information on the change that occurred, given the
        ///       type of change that you wish to monitor and the time (in milliseconds) to wait before timing out.
        ///    </para>
        /// </devdoc>
        public WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType, int timeout) {
            FileSystemEventHandler dirHandler = new FileSystemEventHandler(this.OnInternalFileSystemEventArgs);
            RenamedEventHandler renameHandler = new RenamedEventHandler(this.OnInternalRenameEventArgs);

            this.isChanged = false;
            this.changedResult = WaitForChangedResult.TimedOutResult;

            // Register the internal event handler from the given change types.
            if ((changeType & WatcherChangeTypes.Created) != 0) {
                this.Created += dirHandler;
            }
            if ((changeType & WatcherChangeTypes.Deleted) != 0) {
                this.Deleted += dirHandler;
            }
            if ((changeType & WatcherChangeTypes.Changed) != 0) {
                this.Changed += dirHandler;
            }
            if ((changeType & WatcherChangeTypes.Renamed) != 0) {
                this.Renamed += renameHandler;
            }

            // Save the Enabled state of this component to revert back to it later (if needed).
            bool savedEnabled = EnableRaisingEvents;
            if (savedEnabled == false) {
                runOnce = true;
                EnableRaisingEvents = true;
            }

            // For each thread entering this wait loop, addref it and wait.  When the last one
            // exits, reset the waiterObject.
            WaitForChangedResult retVal = WaitForChangedResult.TimedOutResult;
            lock (this) {
                if (timeout == -1) {
                    while (!isChanged) {
                        System.Threading.Monitor.Wait(this);
                    }
                }
                else {
                    System.Threading.Monitor.Wait(this, timeout, true);
                }

                retVal = changedResult;
            }

            // Revert the Enabled flag to its previous state.
            EnableRaisingEvents = savedEnabled;
            runOnce = false;

            // Decouple the event handlers added above.
            if ((changeType & WatcherChangeTypes.Created) != 0) {
                this.Created -= dirHandler;
            }
            if ((changeType & WatcherChangeTypes.Deleted) != 0) {
                this.Deleted -= dirHandler;
            }
            if ((changeType & WatcherChangeTypes.Changed) != 0) {
                this.Changed -= dirHandler;
            }
            if ((changeType & WatcherChangeTypes.Renamed) != 0) {
                this.Renamed -= renameHandler;
            }

            // Return the struct.
            return retVal;
        }
        private void SignalFileChangeForWaiters(WatcherChangeTypes type, string filePath)
        {
            if (_waiters == 0) return; // No point signaling if no one is waiting

            // Getting the 'relative path' of the filePath compared to the currently monitored folder path
            string uppercaseFilePath = filePath.ToUpperInvariant();
            var fileNameToReport = uppercaseFilePath.Replace(_uppercasePath, string.Empty);

            lock (_latestChangeLocker)
            {
                _latestChange = new WaitForChangedResult() { ChangeType = type, Name = fileNameToReport };
            }
            _changesWatchingEvent.Set();
        }
        public FileSystemPoller(int pollingInterval)
        {
            _path = String.Empty;
            
            _initialFilesSeen = new ManualResetEvent(false);
            _lastSeenFiles = new HashSet<string>();
            _lastSeenDirs = new HashSet<string>();

            Filter = string.Empty;

            _waiters = 0;
            _changesWatchingEvent = new AutoResetEvent(false);
            _latestChangeLocker = new object();
            _latestChange = new WaitForChangedResult();

            _pollingTaskLock = new object();
            PollingInterval = pollingInterval;
            _pollDone = new ManualResetEventSlim(false);
            _pollingTimeoutEvent = new AutoResetEvent(false);
            _pollingEnabledEvent = new ManualResetEventSlim(true);

            PollingType = PollingType.Watch;

        }
Exemple #16
0
        public WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType, int timeout)
        {
            FileSystemEventHandler handler  = new FileSystemEventHandler(this.OnInternalFileSystemEventArgs);
            RenamedEventHandler    handler2 = new RenamedEventHandler(this.OnInternalRenameEventArgs);

            this.isChanged     = false;
            this.changedResult = WaitForChangedResult.TimedOutResult;
            if ((changeType & WatcherChangeTypes.Created) != 0)
            {
                this.Created += handler;
            }
            if ((changeType & WatcherChangeTypes.Deleted) != 0)
            {
                this.Deleted += handler;
            }
            if ((changeType & WatcherChangeTypes.Changed) != 0)
            {
                this.Changed += handler;
            }
            if ((changeType & WatcherChangeTypes.Renamed) != 0)
            {
                this.Renamed += handler2;
            }
            bool enableRaisingEvents = this.EnableRaisingEvents;

            if (!enableRaisingEvents)
            {
                this.runOnce             = true;
                this.EnableRaisingEvents = true;
            }
            WaitForChangedResult timedOutResult = WaitForChangedResult.TimedOutResult;

            lock (this)
            {
                if (timeout == -1)
                {
                    while (!this.isChanged)
                    {
                        System.Threading.Monitor.Wait(this);
                    }
                }
                else
                {
                    System.Threading.Monitor.Wait(this, timeout, true);
                }
                timedOutResult = this.changedResult;
            }
            this.EnableRaisingEvents = enableRaisingEvents;
            this.runOnce             = false;
            if ((changeType & WatcherChangeTypes.Created) != 0)
            {
                this.Created -= handler;
            }
            if ((changeType & WatcherChangeTypes.Deleted) != 0)
            {
                this.Deleted -= handler;
            }
            if ((changeType & WatcherChangeTypes.Changed) != 0)
            {
                this.Changed -= handler;
            }
            if ((changeType & WatcherChangeTypes.Renamed) != 0)
            {
                this.Renamed -= handler2;
            }
            return(timedOutResult);
        }
 static WaitForChangedResult()
 {
     TimedOutResult = new WaitForChangedResult(0, null, true);
 }