From: vani on
What is the best way to lock an xml file if there are multiple aspx pages
and/or ascx controls trying to write to it at the same time?
I already got a suggestion to use FileShare.None on a filestream that is
passed to XDocument, but I would prefer a solution that blocks/waits.

From: Brian Cryer on
"vani" <ivan.svaljek(a)email.t-com.hr> wrote in message
news:i2u25a$fah$1(a)sunce.iskon.hr...
> What is the best way to lock an xml file if there are multiple aspx pages
> and/or ascx controls trying to write to it at the same time?
> I already got a suggestion to use FileShare.None on a filestream that is
> passed to XDocument, but I would prefer a solution that blocks/waits.

Are all of these reads under your control? If so then a mutex based on the
file name might be one way - so you only attempt to open the file if you can
get the mutex. But obviously this won't work unless you can do this on every
page and control which is trying to access the page.

Alternatively (and I'm sure you've already done this) consider why you need
to block and whether there is a different approach which will give you the
functionality you require without requiring blocking.
--
Brian Cryer
http://www.cryer.co.uk/brian

From: Registered User on
On Fri, 30 Jul 2010 10:07:35 +0100, "Brian Cryer" <not.here(a)localhost>
wrote:

>"vani" <ivan.svaljek(a)email.t-com.hr> wrote in message
>news:i2u25a$fah$1(a)sunce.iskon.hr...
>> What is the best way to lock an xml file if there are multiple aspx pages
>> and/or ascx controls trying to write to it at the same time?
>> I already got a suggestion to use FileShare.None on a filestream that is
>> passed to XDocument, but I would prefer a solution that blocks/waits.
>
>Are all of these reads under your control? If so then a mutex based on the
>file name might be one way - so you only attempt to open the file if you can
>get the mutex. But obviously this won't work unless you can do this on every
>page and control which is trying to access the page.
>

Create a type to hide the file itself. Public properties and methods
can be added to allow interaction with the file's data. When access is
needed to the file and its data, create an object of the type. The
sample below likely provides a better explanation of what I mean.

regards
A.G.

public class FileContainer
{
private static Mutex mutex = new Mutex();
internal string pathAndFileName;
public FileContainer(string pathAndFileName)
{
this.pathAndFileName = pathAndFileName;
load();
}

private void load()
{
if (File.Exists(pathAndFileName))
{
mutex.WaitOne();
try
{
// read file here
...
}
finally
{
mutex.ReleaseMutex();
}
}
}

private void save()
{
mutex.WaitOne();
try
{
// write file here
...
}
finally
{
mutex.ReleaseMutex();
}
}
// additional methods, members & properties
...
}

From: Brian Cryer on
"Registered User" <n4jvp(a)ix.netcom.com> wrote in message
news:o9b5569tg60v734e8042snj8ht522saiph(a)4ax.com...
> On Fri, 30 Jul 2010 10:07:35 +0100, "Brian Cryer" <not.here(a)localhost>
> wrote:
>
>>"vani" <ivan.svaljek(a)email.t-com.hr> wrote in message
>>news:i2u25a$fah$1(a)sunce.iskon.hr...
>>> What is the best way to lock an xml file if there are multiple aspx
>>> pages
>>> and/or ascx controls trying to write to it at the same time?
>>> I already got a suggestion to use FileShare.None on a filestream that is
>>> passed to XDocument, but I would prefer a solution that blocks/waits.
>>
>>Are all of these reads under your control? If so then a mutex based on the
>>file name might be one way - so you only attempt to open the file if you
>>can
>>get the mutex. But obviously this won't work unless you can do this on
>>every
>>page and control which is trying to access the page.
>>
>
> Create a type to hide the file itself. Public properties and methods
> can be added to allow interaction with the file's data. When access is
> needed to the file and its data, create an object of the type. The
> sample below likely provides a better explanation of what I mean.
>
> regards
> A.G.
>
> public class FileContainer
> {
> private static Mutex mutex = new Mutex();
> internal string pathAndFileName;
> public FileContainer(string pathAndFileName)
> {
> this.pathAndFileName = pathAndFileName;
> load();
> }
>
> private void load()
> {
> if (File.Exists(pathAndFileName))
> {
> mutex.WaitOne();
> try
> {
> // read file here
> ...
> }
> finally
> {
> mutex.ReleaseMutex();
> }
> }
> }
>
> private void save()
> {
> mutex.WaitOne();
> try
> {
> // write file here
> ...
> }
> finally
> {
> mutex.ReleaseMutex();
> }
> }
> // additional methods, members & properties
> ...
> }

That's a lovely way of doing it.
--
Brian Cryer
http://www.cryer.co.uk/brian

From: vani on
> Create a type to hide the file itself. Public properties and methods
> can be added to allow interaction with the file's data. When access is
> needed to the file and its data, create an object of the type. The
> sample below likely provides a better explanation of what I mean.
>
> regards
> A.G.
>
> public class FileContainer
> {
> private static Mutex mutex = new Mutex();
> internal string pathAndFileName;
> public FileContainer(string pathAndFileName)
> {
> this.pathAndFileName = pathAndFileName;
> load();
> }
>
> private void load()
> {
> if (File.Exists(pathAndFileName))
> {
> mutex.WaitOne();
> try
> {
> // read file here
> ...
> }
> finally
> {
> mutex.ReleaseMutex();
> }
> }
> }
>
> private void save()
> {
> mutex.WaitOne();
> try
> {
> // write file here
> ...
> }
> finally
> {
> mutex.ReleaseMutex();
> }
> }
> // additional methods, members & properties
> ...
> }
>

Thanks for the kick, will try.