-1

I am currently in the process of writing a small application in C# to process batches of images and put them into a PDF. Each batch of images is stored in its own folder on a network share. The application will enable users to perform QA checks on a random number of images from a single batch before creating a PDF. At most there will be between 4-6 users running this application on individual desktops with access to the location where the image batches are stored.

The problem I'm running into at the moment is how do I prevent 2 users from processing the same batch? Initially I thought about using FileSystemWatcher to check for last access to each folder, but reading up on how FileSystemWatcher raises events it didn't seem suitable. I've condsidered using polling to check the images in each folder for File access using a filestream, but I don't think that will be suitable either(I may be wrong).

What would be the simplest solution?

  • Don't allow access to the network drive directly. Proxy it through something that only allows a single user to access images in a folder. – TZHX Oct 13 '17 at 08:35
  • Write a `bob.lock` file in the folder to indicate that the folder is being processed before starting to process that folder, and delete it when you are done. _You may also have to consider mechanisms for detecting whether a machine has crashed and manually deleting the lock file._ – mjwills Oct 13 '17 at 08:37

1 Answers1

0

I'd use a lock file with a package like this.

Code is quite simple:

var fileLock = new SimpleFileLock("networkFolder/file.lock", TimeSpan.FromMinutes(timeout));

where timeout is used to unlock the folder if the process using it crashed (so that it doesn't stay locked forever).

Then, everytime a process needs to use that directory, you go with a simple check:

if (fileLock.TryAcquireLock())
{
    //Lock acquired - do your work here
}
else
{
    //Failed to acquire lock - SpinWait or do something else
}

Code is taken from the samples on the repo, so that's the way the author suggests using his library.

I had the chance to use it and I found it both useful and reliable.

STT
  • 215
  • 1
  • 11