I am doing a 4 steps of process with a 100+ of XML files and write them into a specific location. SO Initially i used a parallel.for to get each of the file and that Parallel loop calling a method which is in another cs file..my process consists of 4 steps..where the first 3 is fetching and modifying the Elements with some conditions and the 4 rth step is writing that into a location..so for that(4th step alone) i have used a lock, So that at a time a single thread can have rights for text writer..so.1st 9 to 11 files processed without the error with locks...after that i am getting a object ref not set to an instance of the object.. i have used concurrent dictionary and a Class object to hold the Data..THE Class object comes as null after the 9 th file... kindly give me an idea to resolve this.
//Parallel Loop in Form1.cs
XMLDEFN WebXML = new XMLDEFN();
DirectoryInfo dinfo = new DirectoryInfo(@"E:\XMLLIST");
FileInfo[] xmllist = dinfo.GetFiles("*.xml", SearchOption.AllDirectories);
int count1 = xmllist.Count();
object lockobj = new object();
Parallel.For(0, count1, i =>
{
WebXML.FileName = xmllist[i].ToString();
WebXML.Migrate(lockobj, i); //this call goes to the another Project CS file
});
Project2.migartor.cs
public override bool Migrate(object lockobj,int i)
{
ProcessContext();
ProcessTasks();
ProcessServices();
ProcessDataTransfer();
IncludeVersionInfo();
lock (lockobj)
{
Save(lockobj,i);
}
return true;
}
protected void Save(object lockobj,int i)
{
int a = i;
XmlSerializer xmlSerializer = new XmlSerializer(typeof(OBJ_M));
XmlSerializerNamespaces ns = null;
TextWriter txtWriter = null;
ns = new XmlSerializerNamespaces();
ns.Add("", "");
// Create an instance of stream writer.
txtWriter = new StreamWriter(@"d:\temp\" + OBJ.ILBOName + "_temp.xml");
xmlSerializer.Serialize(txtWriter, OBJ, ns);
// Close the stream writer
txtWriter.Close();
txtWriter = null;
xmlSerializer = null;
OBJ = null;
}
THE OBJ is coming as null... oBJ comes with some 10+attributes which is updated in the Above 3 steps.