public void OnFileChanged(TrackedFileEventArgs file) { if (CancellationToken.IsCancellationRequested) { return; } var filePath = file.Path; ICancellableTask task = null; // Check if we have an outstanding line count task. If we don't, kick one off lock (OutstandingLineCounts) { if (!OutstandingLineCounts.ContainsKey(filePath)) { // kick off and add line count task task = CancellableTaskRunner.Run(async token => { await CalculateLineCount(filePath, token); }, onThreadPoolThread: true); OutstandingLineCounts.Add(filePath, task); } } task?.Task.ContinueWith(t => { if (t.IsFaulted) { OnLineCountFailed?.Invoke(new LineCountFailedEventArgs(filePath, t.Exception)); } }); }
private static void StateTracker_OnTrackedFileModified(TrackedFileEventArgs eventArgs) { Task.Run(() => { Console.WriteLine($"Tracked file Modified: {eventArgs.Path} at {eventArgs.TimeStamp.TimeOfDay}"); }); }
protected void CheckForDeletedFiles(DirectoryScannedEventArgs eventArgs) { foreach (var missingFilePath in TrackedPaths.Keys.Except(eventArgs.FilePathModificationTimes.Keys).ToList()) { var missingFileEventArgs = new TrackedFileEventArgs(missingFilePath); TrackedPaths.Remove(missingFilePath); OnTrackedFileMissing?.Invoke(missingFileEventArgs); } }
public void OnFileRemoved(TrackedFileEventArgs file) { var filePath = file.Path; // Cancel an outstanding line count task for this file, if one exists lock (OutstandingLineCounts) { if (OutstandingLineCounts.TryGetValue(filePath, out var task)) { task.Cancel(); } } lock (PathLineCounts) { // NOTE: if the key doesn't exist, the method will return null, // not throw an exception, per method documentation PathLineCounts.Remove(filePath); } }
protected void CheckForNewOrModifiedFiles(DirectoryScannedEventArgs eventArgs) { foreach (var filePathModificationTime in eventArgs.FilePathModificationTimes) { var trackedFileEventArgs = new TrackedFileEventArgs(filePathModificationTime.Key); if (TrackedPaths.TryGetValue(filePathModificationTime.Key, out var value)) { if (value < filePathModificationTime.Value) { OnTrackedFileModified?.Invoke(trackedFileEventArgs); } TrackedPaths[filePathModificationTime.Key] = filePathModificationTime.Value; } else { TrackedPaths.Add(filePathModificationTime); OnNewFileScanned?.Invoke(trackedFileEventArgs); } } }