Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

c# - Lock an XML file

We have a requirement as follows for handling XML files in C# code. Please provide logic/solution in such a way to achieve this.

Problem: We have an XML file,assume it has test.xml. When a user 'A' working with test.xml then we want the requirement that to lock this XML and to process like read/write etc. In the sametime a user 'B' also tried to access the test.xml , we want the requirement that user 'B' should not able to open and 'B' has to wait until the lock of user 'A' is to be release or unlock.

Also we shouldn't get any error/exception till the user 'B' is in waiting mode and we don't want to show any message to the user . As we are not interested to use FileStreams in this scenario due to performance issue.

Can anyone help in this regard how to overcome this type of issue.

Please Note: We do not want to use filestreams. Please suggest an alternative solution in this regard.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The technique I am familiar with is to create a lock file as a sibling to the file you are locking. All clients must create this lock file if they want read/write permission on the file in question, and cannot read/write the file if they did not create the lock file. When they are done reading/writing, they close access to the file and delete the lock file. Clients waiting to write, then, should poll for the existence of the lock file, and when it does not exist, they create it and use the file in question freely.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...