Locking xml file

D

dchadha

Hi,

I am working on application in C# which uses and stores data in xml
file. This app can use xml file from two different ways: (a) From User
Interface (Windows application) and (b) From Windows Service.

Both apps read data from xml file in dataset and update the file after
doing the desired operation on dataset. Now the problem is when windows
service reads the xml in dataset and while its doing the operation (it
takes few minutes) before updating the xml file, if I make any change
in xml file using windows app(UI), the changes are not applied as the
windows service writes the xml file from the dataset after the windows
app.

I hope I am able to explain my issue here.

Now what is the best way of avoiding it. I think if I am able to lock
the xml file while any of these two app is working on it, may be I will
solve this problem. But I don't know how to do that.

Thanks
Dinesh
 
B

Barry Kelly

I am working on application in C# which uses and stores data in xml
file. This app can use xml file from two different ways: (a) From User
Interface (Windows application) and (b) From Windows Service.

Both apps read data from xml file in dataset and update the file after
doing the desired operation on dataset. Now the problem is when windows
service reads the xml in dataset and while its doing the operation (it
takes few minutes) before updating the xml file, if I make any change
in xml file using windows app(UI), the changes are not applied as the
windows service writes the xml file from the dataset after the windows
app.

I hope I am able to explain my issue here.

Now what is the best way of avoiding it. I think if I am able to lock
the xml file while any of these two app is working on it, may be I will
solve this problem. But I don't know how to do that.

Locking will give you exceptions when your applications conflict.
FileStream.Lock() and FileStream.Unlock() are one route to fine-grained
locking, while the FileStream constructors which take a FileShare
argument are a coarse-grained route. Either way, you'll need to keep the
FileStream around and alive for as long as you want the file locked, and
dispose it when you're done.

If mutual exclusion of access to the file is what you want, perhaps a
named mutex (the Mutex class with a constructor taking a String) is what
you're looking for. That will cause each application to block if the
other has acquired the mutex. Mutexes are a bit like the "lock" keyword
but for interprocess communication.

-- Barry
 
D

dchadha

Hi Barry,

First of all Thanks for a quick and detailed reply. I am trying to
implement the first approach but not sure how to do it. Following is
how I am reading and writing XML file. Now how should I implement the
FileStream.Lock() Unlock() here.

public static void LockUnlockFile(string xmlFile)
{
ds.ReadXml(xmlFile);
// I want to lock xmlFile here
.....
... write some data to ds...
...
ds.WriteXml(xmlFile);
//I want to unlock xmlFile here
}

Now, should I create FileStream object at both the places ?

Thanks
Dinesh

Barry Kelly wrote:
 
B

Barry Kelly

Hi Barry,

First of all Thanks for a quick and detailed reply. I am trying to
implement the first approach but not sure how to do it. Following is
how I am reading and writing XML file. Now how should I implement the
FileStream.Lock() Unlock() here.

public static void LockUnlockFile(string xmlFile)
{
ds.ReadXml(xmlFile);
// I want to lock xmlFile here
.....
... write some data to ds...
...
ds.WriteXml(xmlFile);
//I want to unlock xmlFile here
}

Now, should I create FileStream object at both the places ?

Consider creating a FileStream at the start, inside a using block, and
read and write with the stream, not with the file:

using (FileStream fs = new FileStream(xmlFile, ...))
{
ds.ReadXml(fs);
// ...
fs.SetLength(0);
ds.WriteXml(fs);
}

-- Barry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top