Solved

Protecting file / multi read and write C# / VB.Net

Posted on 2013-01-09
5
435 Views
Last Modified: 2013-01-26
Hi
Here's my scenario.

1) there is a small file in app_data say "MyFile.xml" for use in users session
2) this file is read and contents filtered/put into dictionary in each users session (to avoid multi-read for such small data) when they visit page Startup.aspx

3) this file can be changed by any user on a different page, say Config.aspx

I guess I need to use threads to prevent Read/Write conflicts between users reading file when visiting Startup.aspx and a user writing on Config.aspx, and 2 or more users trying to change/read file at same time?

if so how - is reasoning right?

Thanks
0
Comment
Question by:rwallacej
5 Comments
 
LVL 9

Expert Comment

by:sognoct
ID: 38758164
Why don't you put the data in a sql table ? It will solve easily all kind of conflicts.

For using xml file in read only mode there are no issues.
But, as you correctly said, if 2 users want to modifify the data you have to use file lock mechanism, to prevent concurrent write, which is sometimes faulty on NTFS.
0
 
LVL 23

Accepted Solution

by:
Michael74 earned 300 total points
ID: 38758374
A file lock can achieved using a mutex. This allows you lock the file across the whole system.

With a Mutex class, you call the WaitOne method to lock and ReleaseMutex to unlock. Closing or disposing a Mutex automatically releases it. Just as with the lock statement, a Mutex can be released only from the same thread that obtained it.
http://www.albahari.com/threading/part2.aspx#_Mutex

A rough example would be
mutex.WaitOne();
MyCommonMethod();
mutex.ReleaseMutex();

Open in new window


Note: If you have lots of users that require access to the file then this will be a major bottleneck and slow down the system. In this case a database will be the better method

Michael
0
 

Author Comment

by:rwallacej
ID: 38758526
Thanks.

Database is not option unfortunately - I did wonder about using this but the (small) data amount has to be easily shared amongst different PCs not necessarily running database via e.g. email and XML file was used as option
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 200 total points
ID: 38759429
If you use a FileStream to read and write the file, you can specify the FileShare value so that file can still be read while its being written.

http://msdn.microsoft.com/en-us/library/system.io.fileshare.aspx

http://msdn.microsoft.com/en-us/library/system.io.filestream.aspx
0
 

Author Closing Comment

by:rwallacej
ID: 38821992
great thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question