public async Task <string> GetChangedFileAsync(CancellationToken cancellationToken) { foreach (var file in _fileSet) { _fileWatcher.WatchDirectory(Path.GetDirectoryName(file)); } var tcs = new TaskCompletionSource <string>(); cancellationToken.Register(() => tcs.TrySetResult(null)); Action <string> callback = path => { if (_fileSet.Contains(path)) { tcs.TrySetResult(path); } }; _fileWatcher.OnFileChange += callback; var changedFile = await tcs.Task; _fileWatcher.OnFileChange -= callback; return(changedFile); }
public async Task <FileItem?> GetChangedFileAsync(CancellationToken cancellationToken, Action startedWatching) { foreach (var file in _fileSet) { _fileWatcher.WatchDirectory(Path.GetDirectoryName(file.FilePath)); } var tcs = new TaskCompletionSource <FileItem?>(TaskCreationOptions.RunContinuationsAsynchronously); cancellationToken.Register(() => tcs.TrySetResult(null)); void FileChangedCallback(string path, bool newFile) { if (_fileSet.TryGetValue(path, out var fileItem)) { tcs.TrySetResult(fileItem); } } _fileWatcher.OnFileChange += FileChangedCallback; startedWatching(); var changedFile = await tcs.Task; _fileWatcher.OnFileChange -= FileChangedCallback; return(changedFile); }