예제 #1
0
 static void OnFileWatcherDirectoryCreatedEvent(object sender, DirectoryCreatedEventArgs e)
 {
     lock (Lock)
     {
         Console.WriteLine("OnDirectoryCreatedEvent:");
         Console.WriteLine("  FilePath = {0}", e.FilePath);
         Console.WriteLine();
     }
 }
        private void OnFileCreated(object sender, FileSystemEventArgs e)
        {
            var filePath = e.FullPath;

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

            if (isDirectory)
            {
                var directoryCreatedEventArgs = new DirectoryCreatedEventArgs(e.FullPath, UserState);
                RaiseAsynchronousOnDirectoryCreatedEvent(directoryCreatedEventArgs);
                return;
            }

            if (!ShouldMonitorFile(filePath)) return;

            if (e.ChangeType != WatcherChangeTypes.Created) return;
            lock (_filesRaisingEventsLock)
            {
                if (_filesChanging.ContainsKey(filePath))
                {
                    _filesChanging[filePath].FileEventType = FileEventType.Created;
                    Touch(filePath);
                }
                else
                {
                    Push(filePath, FileEventType.Created);
                }
            }

            var fileCreatedEventArgs = new FileCreatedEventArgs(e.FullPath, UserState);
            RaiseAsynchronousOnFileCreatedEvent(fileCreatedEventArgs);
        }
 /// <summary>
 /// Template method to add default behaviour for the event
 /// </summary>
 private void OnDirectoryCreatedEvent(DirectoryCreatedEventArgs e)
 {
     // TODO: Implement default behaviour of OnDirectoryCreatedEvent
 }
        /// <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 RaiseOnDirectoryCreatedEvent(DirectoryCreatedEventArgs 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.

            DirectoryCreatedEventHandler eventHandler;

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

            OnDirectoryCreatedEvent(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 RaiseCrossThreadOnDirectoryCreatedEvent(DirectoryCreatedEventArgs e)
 {
     _asyncOperation.SynchronizationContext.Send(new SendOrPostCallback(AsynchronousOnDirectoryCreatedEventRaised), 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 RaiseAsynchronousOnDirectoryCreatedEvent(DirectoryCreatedEventArgs e)
 {
     _asyncOperation.Post(new SendOrPostCallback(AsynchronousOnDirectoryCreatedEventRaised), e);
 }