Reputation: 11304
I have below piece of code which runs every 30 sec. through a C# timer,
var sourceFiles = Directory.GetFiles(sourcePath, "*.zip").Select(f => new FileInfo(f));
foreach (var sourceFile in sourceFiles)
{
var success = await ProcessFileAsync(sourceFile);
}
Sometime in sourcePath
path I have large amount of files and looks like it is not complete in 30 sec and other timer starts. Due to this often I am getting file in use error,
The process cannot access the file 'D:\path\file1.Zip' because it is being used by another process.
How to deal with this? Please suggest.
Upvotes: 0
Views: 49
Reputation: 14700
A common pattern I've used is to mark files that are being processed so other processes won't try to touch them - this is also useful if you plan to parallelize your processing.
var sourceFiles = Directory.GetFiles("*.zip");
foreach (var sourceFile in sourceFiles)
{
var tempFilename = sourceFile + ".processing";
try
{
if (File.Exists(sourceFile))
{
File.Move(sourceFile, tempFilename);
var success = await ProcessFileAsync(sourceFile);
if (success)
{
File.Move(tempFilename, sourceFile + ".done");
}
else
{
File.Move(tempFilename, sourceFile);
}
}
}
catch
{
// Rename the file back to be attempted later.
File.Move(tempFilename, sourceFile);
}
}
Upvotes: 1