static void OnFilesFinishedChangingEvent(object sender, FilesFinishedChangingEventArgs e) { lock (Lock) { Console.WriteLine("OnFilesFinishedChangingEvent:"); foreach (var fileFinishedChangingEventArgs in e.FilesFinishedChanging) { Console.WriteLine(" FilePath = {0}", fileFinishedChangingEventArgs.FilePath); Console.WriteLine(" ChangeType = {0}", Enum.GetName(typeof (FileEventType), fileFinishedChangingEventArgs.ChangeType)); } Console.WriteLine(); } }
/// <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 RaiseOnFilesFinishedChangingEvent(FilesFinishedChangingEventArgs 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. FilesFinishedChangingEventHandler eventHandler; if (!Monitor.TryEnter(_filesFinishedChangingEventLock, _lockTimeout)) { throw new ApplicationException("Timeout waiting for lock - RaiseOnFilesFinishedChangingEvent"); } try { eventHandler = _filesFinishedChangingEvent; } finally { Monitor.Exit(_filesFinishedChangingEventLock); } OnFilesFinishedChangingEvent(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 RaiseCrossThreadOnFilesFinishedChangingEvent(FilesFinishedChangingEventArgs e) { _asyncOperation.SynchronizationContext.Send(new SendOrPostCallback(AsynchronousOnFilesFinishedChangingEventRaised), 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 RaiseAsynchronousOnFilesFinishedChangingEvent(FilesFinishedChangingEventArgs e) { _asyncOperation.Post(new SendOrPostCallback(AsynchronousOnFilesFinishedChangingEventRaised), e); }
/// <summary> /// Template method to add default behaviour for the event /// </summary> private void OnFilesFinishedChangingEvent(FilesFinishedChangingEventArgs e) { // TODO: Implement default behaviour of OnFilesFinishedChangingEvent }
private void OnFileFinishedChanging(FileFinishedChangingEventArgs e) { lock (_filesRaisingEventsLock) { var filePath = e.FilePath; if (_filesChanging.ContainsKey(filePath)) { if ( IsFileLocked(filePath)) { //The file is still currently in use, lets try raise the event later Touch(filePath); } else { Pop(filePath); var fileFinishedChangingEventArgs = new FileFinishedChangingEventArgs(e.FilePath, e.ChangeType, UserState); //We only want to know about the last event, not any that may have happened in the mean time _filesFinishedChanging[fileFinishedChangingEventArgs.FilePath] = fileFinishedChangingEventArgs; RaiseAsynchronousOnFileFinishedChangingEvent(fileFinishedChangingEventArgs); if (_filesChanging == null || _filesChanging.Count < 1) { if (_filesFinishedChanging != null && _filesFinishedChanging.Count > 0) { //There are no more files that are in the change queue so let everyone know the files have finished changing var filesFinishedChangingEventArgs = new FilesFinishedChangingEventArgs(new List<FileFinishedChangingEventArgs>(_filesFinishedChanging.Values), UserState); RaiseAsynchronousOnFilesFinishedChangingEvent(filesFinishedChangingEventArgs); _filesFinishedChanging = new Dictionary<string, FileFinishedChangingEventArgs>(); } } } } } }