user584018
user584018

Reputation: 11304

How to process bunch of files at once without process lock

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

Answers (1)

Avner Shahar-Kashtan
Avner Shahar-Kashtan

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

Related Questions