1

I currently write a program containing a FileSystemWatcher which watches a network path.

The files are all .txt's. Most files are between 200k and 3 MB, since I get errors if the file is saved directly into the watched folder, instead of being copied, I am building a timer based on this answer:

FileSystemWatcher fires before file is saved - how do you "pause" the process?

Now there is the question in room, I need those Event handled very fast, so should i really use the 1000 ms the user mentioned or is a lower value OK? More specific: Whats is the minimum value I should choose to, on the one side, be fast and on the other be still save ?

Maybe there is a better solution for my problem?

€: I now choose to use https://stackoverflow.com/a/3822355/3664953 as a solution. Since there is the possibility I have to handle a few files at once, I choose to use this in a TPL. The Code I use in the Event:

private void OnCreate(object sender, FileSystemEventArgs e)
{
  var fi = new FileInfo(e.FullPath);
  Console.WriteLine(e.Name + " " + DateTime.Now);
  System.Threading.Tasks.Task.Factory.StartNew(() =>
    {
      DoWork(fi);
    }
  );
}

The Code I use to not run into an Exception for being too fast:

private void DoWork(FileInfo file)
{
  var sw = new System.Diagnostics.Stopwatch();
  sw.Start();
  while(true)
  {
    if (IsFileLocked(file) == false)
    {
      break;
    }
    if(sw.ElapsedMilliseconds >= 30000)
    {
       _log.Status(Log.LL.Error, String.Format("Datei {0} ist gesperrt und kann nicht   
                    importiert werden.", file.Name));
       return; 
    }
  }
...

At the Moment the only Problem I run into is, if there are like 100 files created/copied it gets INCREDIBLY slow, which causes my MSSQL to return a timeout later on. But that isn't a problem I have to handle :)

Ty for pointing me in the right direction Neolisk.

Community
  • 1
  • 1
ExNought
  • 193
  • 1
  • 11
  • 1
    possible duplicate of [Filesystem watcher and large files](http://stackoverflow.com/questions/3822175/filesystem-watcher-and-large-files) – Victor Zakharov Sep 15 '14 at 13:34
  • 2
    There is no point in doing anything fancy when the FSW event fires, the odds that you can access the file are very slim. So just don't bother and add the path of the file to a thread-safe collection. You can do that *very* fast. Whatever you do later can take its merry time, including attempting to access the file which can take minutes or hours. – Hans Passant Sep 15 '14 at 13:47
  • @Neolisk - note that accepted solution to the linked question does not provide solution but rather infinite synchronous wait... Combined with Hans Passant's suggestion of having separate queue of files to check it may work. (Note it would be interesting if there is `async` code that waits till file is no longer locked - may result in nicer looking code). – Alexei Levenkov Sep 15 '14 at 14:32
  • @AlexeiLevenkov: I did not say all possible solutions are pretty. Although [this one](http://stackoverflow.com/a/3822261/897326) is, maybe I'm missing something - did not read it all. The point was that there are multiple ways of solving this problem, how the OP wants to do it is a decision point. – Victor Zakharov Sep 15 '14 at 15:32

0 Answers0