Solved

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

Posted on 2013-01-09
5
447 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

How our DevOps Teams Maximize Uptime

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

Question has a verified solution.

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

Suggested Solutions

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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