1

I'm creating a File Watcher BackgroundService where once a file has been moved to my target folder, I need to give it X minutes before I then processed with my application.

I have the file watcher set up on my target folder like so:

public void WatchTargetFolder(string folderLocation, string fileName = "")
{
    if (fileName == "") fileName = "Test";

     _watcher.Path = folderLocation;
     _watcher.NotifyFilter = NotifyFilters.LastWrite;
     _watcher.Filter = $"{fileName}.xlsx";
     _watcher.Changed += OnChanged;
     _watcher.EnableRaisingEvents = true;
}

private void OnChanged(object source, FileSystemEventArgs e)
{

}

What is the best way to track the time since my file was last written to (as I've got in my NotifyFilter)? So that once the time has passed, my file will then move.

N0xus
  • 2,674
  • 12
  • 65
  • 126
  • What about using Task.Delay in your OnChanged handler - see https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.delay?view=netcore-3.1 – auburg May 27 '20 at 14:01
  • 1
    Is [this](https://stackoverflow.com/q/10982104/1997232) a problem you are trying to solve? – Sinatr May 27 '20 at 14:04
  • Is your question related to this? [FileSystemWatcher Changed event is raised twice](https://stackoverflow.com/questions/1764809/filesystemwatcher-changed-event-is-raised-twice). If yes, I have posted there an [answer](https://stackoverflow.com/questions/1764809/filesystemwatcher-changed-event-is-raised-twice/58079327#58079327) that allows to subscribe to multiple `FileSystemWatcher` events, and get delayed notification about the last one that occurred, similar in functionality to the RX [`Debounce`](http://reactivex.io/documentation/operators/debounce.html) operator. – Theodor Zoulias May 28 '20 at 11:18

0 Answers0