This project is read-only.

Lock on file being downloaded

Sep 12, 2012 at 9:52 AM


I'm using to download files within a windows service. The windows service is basically a timer/scheduler that trigger the sftp job every x minutes. The job tries to connect to the remote folder and check if there are any files then proceed to download them. Most of the time the file size is small (~100kb) but occasionally there will be a very large file (~1GB), it will take quite a while to finish download the large file. By then subsequent job runs would have tried to download the same big file; so is there any way to put a lock on the file being downloaded? or if not, any way we can know the file is being downloaded currently by same user?



Sep 12, 2012 at 9:59 AM
Edited Sep 12, 2012 at 10:00 AM
  1. Use a named mutex to block 2nd run from beginning the download operation. A good tip is to write to the Windows Event Log to log that the 2nd run is not starting up because of the 1st run.
    1. Another option is to use the remote filename as a mutex so that only files being downloaded get's skipped if the mutex exists. 
  2. Keep session data for the service, via a pipe or file. If using a file, you need to think about how to securely access your session data, so the 2nd run doesnt overwrite the 1st run's session.
  3. Create a file on the remote server "filename.ext.lock", and remove it when done downloading.

SSH.NET provides the protocol support, your app needs to manage a state, on how SSH.NET is used, what files are downloaded etc.

Sep 12, 2012 at 10:19 AM

Kenneth, thanks for the fast reply.

option 3) seems to be easiest to implement. So I just need to rename the file being downloaded, myfile.ext -> myfile.ext.lock. and change my program to not pick up files with .lock