Ejemplo n.º 1
0
 static void OnFileCreatedEvent(object sender, FileCreatedEventArgs e)
 {
     lock (Lock)
     {
         Console.WriteLine("OnFileCreatedEvent:");
         Console.WriteLine("  FilePath = {0}", e.FilePath);
         Console.WriteLine();
     }
 }
 /// <summary>
 /// Template method to add default behaviour for the event
 /// </summary>
 private void OnFileCreatedEvent(FileCreatedEventArgs e)
 {
     // TODO: Implement default behaviour of OnFileCreatedEvent
 }
        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>
        /// 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 RaiseOnFileCreatedEvent(FileCreatedEventArgs 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.

            FileCreatedEventHandler eventHandler;

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

            OnFileCreatedEvent(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 RaiseCrossThreadOnFileCreatedEvent(FileCreatedEventArgs e)
 {
     _asyncOperation.SynchronizationContext.Send(new SendOrPostCallback(AsynchronousOnFileCreatedEventRaised), 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 RaiseAsynchronousOnFileCreatedEvent(FileCreatedEventArgs e)
 {
     _asyncOperation.Post(new SendOrPostCallback(AsynchronousOnFileCreatedEventRaised), e);
 }