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.