views:

223

answers:

6

We are monitoring the progress of a customized app (whose source is not under our control) which writes to a XML Manifest. At times , the application is stuck due to unable to write into the Manifest file. Although we are covering our traces by explicitly closing the file handle using File.Close and also creating the file variables in Using Blocks. But somehow it keeps happening. ( Our application is multithreaded and at most three threads might be accessing the file. ) Another interesting thing is that their app updates this manifest at three different events(add items, deleting items, completion of items) but we are only suffering about one event (completion of items). My code is listed here

using (var st = new FileStream(MenifestPath, FileMode.Open, FileAccess.Read))
{
    using (TextReader r = new StreamReader(st))
    {
       var xml = r.ReadToEnd();
           r.Close();
           st.Close();
          //................ Rest of our operations
    }
}
+2  A: 

hope the following link helps.

http://stackoverflow.com/questions/119548/problem-in-writing-to-single-file-in-web-service-in-net

pradeeptp
Exactly the right thing to do - using locking so the threads aren't trying to access the file at the same time. Worked for me when I have multiple threads logging to the same file. Create a library to manage accessing the file and you are golden.
ScottCher
A: 

The problem is different because that person is having full control on the file access for all processes while as i mentioned ONE PROCESS IS THIRD PARTY WITH NO SOURCE ACCCESS. And our applications are working fine. However, their application seems stuck if they cant get hold the control of file. So i am willing to find a method of file access that does not disturb their running.

Gripsoft
+1  A: 

If you are only reading from the file, then you should be able to pass a flag to specify the sharing mode. I don't know how you specify this in .NET, but in WinAPI you'd pass FILE_SHARE_READ | FILE_SHARE_WRITE to CreateFile().

I suggest you check your file API documentation to see where it mentions sharing modes.

MrZebra
Yup There is a FileShare Constant. Thanks i will try that and see how it works
Gripsoft
Thanks Adding a FileShare constant both application's thread seems understanding each other. As our application was just reading so i just used READ_WRITE attribute for other files.Hence their application always open the file .
Gripsoft
+1  A: 

Two things:

  1. You should do the rest of your operations outside the scopes of the using statements. This way, you won't risk using the closed stream and reader. Also, you needn't use the Close methods, because when you exit the scope of the using statement, Dispose is called, which is equivalent.
  2. You should use the overload that has the FileShare enumeration. Locking is paranoid in nature, so the file may be locked automatically to protect you from yourself. :)

HTH.

Omer van Kloeten
A: 

This could happen if one thread was attempting to read from the file while another was writing. To avoid this type of situation where you want multiple readers but only one writer at a time, make use of the ReaderWriterLock or in .NET 2.0 the ReaderWriterLockSlim class in the System.Threading namespace.

jezell
A: 

Also, if you're using .NET 2.0+, you can simplify your code to just:

string xmlText = File.ReadAllText(ManifestFile);

See also: File.ReadAllText on MSDN.

tshak