Multi-thread file access (locking control)

一笑奈何 提交于 2019-12-13 01:26:07

问题


i have two programs working on the same file i want the first program to check regularly on the file, and if any changes are made operate on the file and empty it. the other program writes to the file. the problem occurs when the second program tries to write on the file because it's used from the first one is there an (if there's any implemented C# program would be even better) algorithm to handle this?


回答1:


To check if file has changed you might first of all check its timestamp. In pseudo code, you might try something like this in the monitoring program:

try {
  if(FileTimeStampHasChanged(FileName)) {
    //Attempt to do something with the file
  }
}
catch {
  // Handle or ignore the exception if you can't open the file (maybe the other application hasn't finished writing). You can always retry later.
}

However, I would strongly recommend a different approach altogether. Here are the options I'd suggest.

  1. If you can alter the writing program, let it write to a temporary file (e.g. output.$$$) and let it rename it as soon as it's done writing. Renaming is instantaneous and doesn't cause any locking. This is, in my experience, one of the best simple solutions for concurrent access.
  2. If you cannot alter the writing program, then modify the reading one. Using the logic I described in point 1, let it try to rename the file to read and work on the renamed copy. If the rename fails, then it means that the writer isn't done, and an attempt can be done later.
  3. If you cannot alter any of the programs, then you can make your own "traffic warden" application to implement point 1. It requires more work, but it's still doable. Here's how:
    • Configure the Writer to write into a folder, e.g. "Output".
    • Configure the Reader to read from another folder, e.g. "Input".
    • Write your Traffic Warden so that it monitors Output folder and, when the file changes, it moves it to Input folder. Moving is instantaneous as well (it's practically the same operation as renaming).



回答2:


You are looking for opening file in both programs with FileShare.Write or ReadWrite using FileStream constructor.

If you open file this way both will be able to write. Note that you will have to manually implement some synchronization mechanism between 2 programs so they don't overwrite each others data.

Closing file after each write and waiting for file becoming unlocked when opening it may be easier option depending on access pattern.

Note that if you don't have control over one of the programs your only option is to simply wait till it is done with file in your program (you can force other program to close file handle, but you just don't want to do that as it will likely corrupt data).



来源:https://stackoverflow.com/questions/10195246/multi-thread-file-access-locking-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!