Re: lock an xmlfile

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: William LaMartin (lamartin_at_tampabay.rr.com)
Date: 02/02/04


Date: Sun, 1 Feb 2004 19:07:14 -0500

The problem I ran into when using Mutex is that in a shared hosting
environment, its use would throw a security exception. I could never really
get the hosting company to understand the problem. I am not even sure
anyone there understood the question--what needed to be set so as to allow
the use of Mutex. Mutex was not in their vocabulary.

Its use worked fine on my local setup, though. So if you have control over
the server, then I think Mutex is the way to go in restricting the use of
files while they are being updated.

"Peter Rilling" <peter@nospam.rilling.net> wrote in message
news:em7dkPI6DHA.1664@TK2MSFTNGP11.phx.gbl...
> Now you get into more interesting architectural issues.
>
> The simplest way might be a bruit force method in which you use the loop
but
> use a try...catch block so the exception does not bubble (I do not know
how
> to ask the OS if a file is locked).
>
> Another way might be to look into using the Mutex class. You could wrap
> access to the file so that a Mutex is always created when the file is
> opened, then released when closed.
>
> "Shawn" <bossman100@hotmail.com> wrote in message
> news:401b9580$1@news.wineasy.se...
> > Thanks Peter, I think that will do the trick. One thing though. If the
> > file is closed by another FileStream, is there a way I can wait until
the
> > file is no longer locked instead of just writing an error message to the
> > user? Something like this:
> > Do While file.isClosed
> > 'Just waiting for the file to open
> > Loop
> >
> > Thanks,
> > Shawn
> >
> >
> > "Peter Rilling" <peter@nospam.rilling.net> wrote in message
> > news:OFQxbk15DHA.2252@TK2MSFTNGP10.phx.gbl...
> > What happens is that the XML content is loaded entirely in memory and
the
> > DOM is created. So normally there are no connections to the file,
unlike
> > when you have a stream.
> >
> > Have not tried this but what you might try doing is to manually open the
> > file using the FileStream class (you can specify locks using the
FileShare
> > enumeration), then use that stream when you call the XmlDocument.Load.
> >
> >
> >
> > "Shawn" <bossman100@hotmail.com> wrote in message
> > news:401a8d8f$1@news.wineasy.se...
> > > Hi.
> > > I have a webservice method that receives an XmlNode. It then loads an
> > > existing xml file into an XmlDocument, appends the new XmlNode to the
> > > XmlDocument and saves it back to the xml file. But what if another
user
> is
> > > doing the same thing at the same time? I did a test and found out that
> the
> > > file is not automatically locked. Which means that the second user
> > > overwrites the changes the first user made. Any ideas on how I can
> prevent
> > > this?
> > >
> > > Thanks,
> > > Shawn
> > >
> > >
> > >
> >
> >
> >
>
>



Relevant Pages

  • Re: lock an xmlfile
    ... I've tried looking into Mutex and how to use it to lock a file with VB.NET, ... > The problem I ran into when using Mutex is that in a shared hosting ... >> The simplest way might be a bruit force method in which you use the loop ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Understanding pthread_cond_wait()
    ... Then Thread A signals either Thread B, C or D, sets signal to 0 ... I'll start with say, Thread B that enters the whileloop, locks ... at the same time unlocking the mutex. ...
    (comp.programming.threads)
  • Re: pthread help
    ... to create a thread on a loop function involved in data stream output ... change the buffer location the loop thread use as ... A thread can acquire a mutex, add a job to the linked list, and then ...
    (comp.os.linux.development.apps)
  • =?windows-1252?B?UmU6IGludmFsaWQgY29udmVyc2lvbiBmcm9tIJF2b2lkKiAoKikoKZIgdG8gkQ==?= =?windows
    ... The point of the 'while' loop is to keep looping until you know the ... You just unlocked the mutex. ... you modify 'count' without holding the condition_mutex. ... reading it in the 'while' loop. ...
    (comp.programming.threads)
  • fork and file descriptor
    ... My program has a main loop and a thread. ... The main loop is used to fork a child and the thread is used write data ... to dsp. ... So I am thinking to use mutex to make sure dsp is opened in ...
    (comp.unix.programmer)