简体   繁体   中英

How to prevent more than one application from opening the same file

I have a windows service to upload file to different ftp site.

In order to speed up uploading, this service installed on 3 machines. Then problem occurs, because 3 services all look into same location to pick up file, sometimes they just pick up same file and then exception throws.

How to avoid such situation? the service designed by .net 4.5 with async method, so if file lock applied, is performance will be slower? I am thinking this way,

  1. get all file names in directory
  2. then lock this collection
  3. somehow make sure each service get different sub collection, for example, 30 files total, then each service get 10.

but don't know how.

您可以打开该文件以进行独占读取,如果该文件失败,则表示当前有其他服务正在处理该文件,然后移至下一个文件。

File.Open("test.txt", FileMode.Open, FileAccess.Read, FileShare.None);

You can create a queue. It can be MSMQ or another service, which will load db Table with file names. Your other services can use some lock table.

You would have a table with a single record, which your service will call update table t1 set t1.f1=t1.f1 in transaction. This will prevent other services from continuing.

In another table you have list of files and a flag if it is already taken.

Consider one service only reading these files and loading the file name table. 3 other services - end points, will lock the lock table record and then proceed to reading n file names and mark them as taken. Once files are loaded, file names can be deleted. If the service fails between taken and deleted: at the start of next process, check timestamp. If files were taken, lets say, yesterday and records still exist, may be they need to be processed again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM