开发者

Queue file operations for later when file is locked

开发者 https://www.devze.com 2023-03-05 18:25 出处:网络
I am trying to implement file based autoincrement identity value (at int value stored in TXT file) and I am trying to come up with the best way to handle concurrency issues. This identity will be used

I am trying to implement file based autoincrement identity value (at int value stored in TXT file) and I am trying to come up with the best way to handle concurrency issues. This identity will be used for unique ID for my content. When saving new content this file gets opened, the value gets read, incremented, new content is saved and the incremented value is written back to the file (whether we store the next available ID or the last issued one doesn't really matter). While this is being done another process might come along and try to save new content. The previous process opens the file with FileShare.None so no other process will be able to read the file until it is released by the first process. While the odds of this happening are minimal it could still happen.

Now when this does happen we have two options:

  1. wait for the file to become available -

Emulate waiting on File.Open in C# when file is locked

we are talking about miliseconds here, so I guess this wouldn't be an issue as long as something strange happens and file nev开发者_运维问答er becomes available, then this solution would result in an infinite loop, so not an ideal solution

  1. implement some sort of a queue and run all operations on files within a queue. My user experience requirements are such that at the time of saving/modifying files user should never be informed about exceptions or that something went wrong - he would get informed about them through a very friendly user interface later when operations would fail on the queue too.

At the moment of writing this, the solution should work within ASP.NET MVC application (both synchronously and async thru AJAX) but, if possible, it should use the concepts that could also work in Silverlight or Windows Forms or WPF application.

With regards to those two options which one do you think is better and for the second option what are possible technologies to implement this?


The ReaderWriterLockSlim class seems like a good solution for synchronizing access to the shared resource.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号