Two programs accessing an XML file as a dataset.

U

UJ

I have two programs that run at the same time. One creates an XML file of a
dataset, the other reads the file. They don't appear to be doing it at the
same time. Every once in a while, the XML file appears to get corrupt and
needs to be recreated. (The one reading gets a message about the root not
being valid.) I've tried putting a mutex in so that I know the file is only
in use by one and that didn't seem to work. I've tried having the writer
create a file under a different name, write the file and then rename it.
Still a problem.

The file isn't terribly big (only about 5 records with 4 fields each) and
I'm getting to the point that I may end up writing the stuff to the registry
because I know that that works.

Has anybody seen anything like this before? I must be missing something.....

TIA - Jeff.
 
M

Marc Gravell

When you say corrupted, what were the file contents? And are you sure the
mutex was shared between the processes?
How are you accessing the files? XmlWriter/XmlReader?
XmlDocument.Save/.Load? what? (e.g. corruption if using XmlWriter could be
due to leaving old data at the end of the file (after your new xml) when
writing to the FileStream).

Also - if the corruption is due to concurrent access, taking out exclusive
file access might help; the second caller would get an exception, but at
least the file would be left intact.

Marc
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


The creator should open the file with exclusive lock, write the data and
close it. Only then the reader could open it. You do not explain how the
reader knows that a new file is ready. You could do so by using
FileSystemWatcher. I recommend to not use the data file itself but another
file with the same name but a different extension (.flag for example) .
 

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