Hi

I have an application which scans a folder for files and creates an XML file for each file in the folder by reading few data from the existing folder. This is a multi threaded application and the user has the option to define the number of threads that he wants to create each time the application runs. Currently, I read the existing file names in the folder into an array list and use an integer variable to scan through the array list. I am using Interlocked.Increment to increment the variable.

Each thread calls a function where, before increment, i assign the existing value in the variable to a local variable and the next line is the increment as below.

intFileIndexLocal = intFileIndex;
Interlocked.Increment(ref intFileIndex);

After this intFileIndexLocal is used by the current thread to get the file name from the array list. It seems to be working perfectly but at times it is failing as 2 threads access the same file and it throws up an exception and freezes when the second thread tries to create the output XML file. The exception is that the XML file is already in use by another process. The user runs the application with about 15-20 threads.

Any help on overcoming this will be of great help. The application is already in production and it is becoming a regular issue as it starts freezing when it hits this exception.

Thanks

Navin