Hi Arjay

Thanks for responding. I will try your recommendations.

Here is what I am doing to create the XML files. The user has reasons to process the same file again. So I first check for the existence of the XML file and delete it and then go on to create the new XML file.

Code:
if (File.Exists(strXmlPath + "\\" + strFileName + ".xml"))
       File.Delete(strXmlPath + "\\" + strFileName + ".xml");

strXmlFilename = strXmlPath + "\\" + strFileName + ".tmp";
Xmlwriter = new XmlTextWriter(strXmlFilename, Encoding.UTF8);
Xmlwriter.Formatting = Formatting.Indented;
Xmlwriter.Indentation = 5;
Xmlwriter.WriteStartDocument();
Now i go on to create the nodes in the XML file and then close the file.

Code:
Xmlwriter.WriteEndDocument();
Xmlwriter.Flush();
Xmlwriter.Close();
//This is rename the .tmp file to xml file
if (File.Exists(strXmlFilename))
{
    File.Move(strXmlFilename, strXmlPath + "\\" + strFileName + ".xml");
}
Please do let me know if you would need any further info on this.

Thanks

Quote Originally Posted by Arjay View Post
Many folks new to multithreading mistakenly believe that they only have to protect the writes of a shared resource. The fact is that the reads and writes both need to be protected.

This is one of the problems with your code.
Code:
intFileIndexLocal = intFileIndex;  // Error: unprotected read access.
Interlocked.Increment(ref intFileIndex);
You can use the Interlocked.Exchange method to read the value in a thread safe manner.
Code:
intFileIndexLocal = Interlocked.Exchanged(ref intFileIndex, intFileIndex);
Another issue could be how you write the file. Can you post the write file code?