예제 #1
0
        private Task ProcessFiles()
        {
            return(Task.Run(async() =>
            {
                var fileInfos = new DirectoryInfo(FilePath).GetFiles(FilePattern);

                foreach (var fileInfo in fileInfos)
                {
                    var newNumLines = await GetLineCount(fileInfo.FullName);
                    var oldFileInfoPair = FileLineMap.FirstOrDefault(fi => fi.Key.Name == fileInfo.Name);

                    if (!oldFileInfoPair.Equals(default(KeyValuePair <FileInfo, int>)) && oldFileInfoPair.Key.LastWriteTime != fileInfo.LastWriteTime)
                    {
                        // update the # of lines (other FileInfo properties will be refreshed in RemoveDeletedFiles)
                        if (FileLineMap.TryUpdate(oldFileInfoPair.Key, newNumLines, oldFileInfoPair.Value))
                        {
                            OutputFunction.Invoke($"Changed: {fileInfo.Name}, {newNumLines - oldFileInfoPair.Value}");
                        }
                    }
                    // new file
                    else
                    {
                        if (oldFileInfoPair.Key?.Name != fileInfo.Name && FileLineMap.TryAdd(fileInfo, newNumLines))
                        {
                            OutputFunction.Invoke($"Created: {fileInfo.Name}, {newNumLines}");
                        }
                    }
                }

                // no need to wait for this
                Task.Factory.StartNew(RemoveDeletedFiles, CancellationToken);
            },
                            CancellationToken));
        }
예제 #2
0
        protected void RemoveDeletedFiles()
        {
            Parallel.ForEach(FileLineMap,
                             async kvp =>
            {
                // new to wait on refresh to determine if the file still exists
                await Task.Run(() => kvp.Key.Refresh(), CancellationToken);

                if (!kvp.Key.Exists && FileLineMap.TryRemove(kvp.Key, out _))
                {
                    OutputFunction.Invoke($"Deleted: {kvp.Key.Name}");
                }
            });
        }
예제 #3
0
        private async void FileEventHandler(object sender, FileSystemEventArgs args)
        {
            await Task.Factory.StartNew(async() =>
            {
                var numLines   = 0;
                var changeType = WatcherChangeTypes.Deleted;

                if (args.ChangeType == WatcherChangeTypes.Changed)
                {
                    changeType = WatcherChangeTypes.Created;

                    var oldNumLines = 0;
                    var newNumLines = await GetLineCount(args.FullPath);

                    var kvp = FileLineMap.FirstOrDefault(i => i.Key.Name == args.FullPath);
                    if (kvp.Key != default(FileInfo))
                    {
                        oldNumLines = kvp.Value;
                        changeType  = WatcherChangeTypes.Changed;
                    }

                    numLines = newNumLines - oldNumLines;
                    FileLineMap[new FileInfo(args.FullPath)] = newNumLines;
                }

                var sb = new StringBuilder($"{changeType}: {args.Name}");

                if (changeType != WatcherChangeTypes.Deleted)
                {
                    sb.Append($", {numLines}");
                }

                OutputFunction.Invoke(sb.ToString());
            },
                                        CancellationToken);
        }