示例#1
0
 static void OnFileChangedEvent(object sender, FileChangedEventArgs e)
 {
     lock (Lock)
     {
         Console.WriteLine("OnFileChangedEvent :");
         Console.WriteLine("  FilePath = {0}", e.FilePath);
         Console.WriteLine();
     }
 }
 /// <summary>
 /// Template method to add default behaviour for the event
 /// </summary>
 private void OnFileChangedEvent(FileChangedEventArgs e)
 {
     // TODO: Implement default behaviour of OnFileChangedEvent
 }
        private void OnFileChanged(object sender, FileSystemEventArgs e)
        {
            var filePath = e.FullPath;

            var isDirectory = Directory.Exists(filePath) && (File.GetAttributes(filePath) & FileAttributes.Directory) == FileAttributes.Directory;

            if (isDirectory)
            {
                return;
            }

            if (!ShouldMonitorFile(filePath)) return;

            lock (_filesRaisingEventsLock)
            {
                if (_filesChanging.ContainsKey(filePath))
                {
                    _filesChanging[filePath].FileEventType = FileEventType.Changed;
                    Touch(filePath);
                }
                else
                {
                    Push(filePath, FileEventType.Changed);
                }
            }

            var fileChangedEventArgs = new FileChangedEventArgs(filePath, UserState);
            RaiseAsynchronousOnFileChangedEvent(fileChangedEventArgs);
        }
        /// <summary>
        /// Will raise the event on the current thread synchronously.
        /// i.e. it will wait until all event handlers have processed the event.
        /// </summary>
        /// <param name="e">The state to be passed to the event.</param>
        private void RaiseOnFileChangedEvent(FileChangedEventArgs e)
        {
            // Make a temporary copy of the event to avoid possibility of
            // a race condition if the last subscriber unsubscribes
            // immediately after the null check and before the event is raised.

            FileChangedEventHandler eventHandler;

            if (!Monitor.TryEnter(_fileChangedEventLock, _lockTimeout))
            {
                throw new ApplicationException("Timeout waiting for lock - RaiseOnFileChangedEvent");
            }
            try
            {
                eventHandler = _fileChangedEvent;
            }
            finally
            {
                Monitor.Exit(_fileChangedEventLock);
            }

            OnFileChangedEvent(e);

            if (eventHandler != null)
            {
                eventHandler(this, e);
            }
        }
 /// <summary>
 /// Will raise the event on the calling thread synchronously. 
 /// i.e. it will wait until all event handlers have processed the event.
 /// </summary>
 /// <param name="state">The state to be passed to the event.</param>
 private void RaiseCrossThreadOnFileChangedEvent(FileChangedEventArgs e)
 {
     _asyncOperation.SynchronizationContext.Send(new SendOrPostCallback(AsynchronousOnFileChangedEventRaised), e);
 }
 /// <summary>
 /// Will raise the event on the calling thread asynchronously. 
 /// i.e. it will immediatly continue processing even though event 
 /// handlers have not processed the event yet.
 /// </summary>
 /// <param name="state">The state to be passed to the event.</param>
 private void RaiseAsynchronousOnFileChangedEvent(FileChangedEventArgs e)
 {
     _asyncOperation.Post(new SendOrPostCallback(AsynchronousOnFileChangedEventRaised), e);
 }