Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

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

Posted on 2013-01-09
5
Medium Priority
?
467 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:
Michael Fowler earned 1200 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 800 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

649 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