c# - TextWriter not writing all files -


i've got written service has separate thread running reads 400 records database , serializes them xml files. runs fine, there no errors , reports files have been exported correctly, yet handful of xml files appear afterwards, , different number each time. i've checked see if it's record causing problems, read out fine, , seem write fin, don't...

after playing around , putting delay in of 250ms between each write exported properly, assume must have writing many files in such quick succession, have no idea why, have thought report kind of error if didn't write properly, yet there's nothing.

here code wants try it:

static void main(string[] args) {     exporttestdata(); }  public static void exporttestdata() {     list<testobject> testobjs = getdata();      foreach (testobject obj in testobjs)     {         exportobj(obj);         //thread.sleep(10);     } }  public static list<testobject> getdata() {     list<testobject> result = new list<testobject>();      (int = 0; < 500; i++)     {         result.add(new testobject()         {             date = datetime.now.adddays(-1),             anotherdate = datetime.now.adddays(-2),             anotheranotherdate = datetime.now,             doubleone = 1.0,             doubletwo = 2.0,             doublethree = 3.0,             number = 345,             somecode = "blah",             someid = "wobble wobble"         });     }      return result; }  public static void exportobj(testobject obj) {     try     {         string path = path.combine(@"c:\temp\exports", string.format("{0}-{1}{2}", datetime.now.tostring("yyyymmdd"), string.format("{0:hhmmssfff}", datetime.now), ".xml"));         serializeto(obj, path);     }     catch (exception ex)     {      } }  public static bool serializeto<t>(t obj, string path) {     xmlserializer xs = new xmlserializer(obj.gettype());     using (textwriter writer = new streamwriter(path, false))     {         xs.serialize(writer, obj);     }     return true; } 

try commenting\uncommenting thread.sleep(10) see problem

does have idea why this? , can suggest how can avoid problem?

thanks

edit: solved. time based filename wasn't unique enough , overwriting written files. should've spotted earlier, help

perhaps try putting writer in using block immediate disposal? like

xmlserializer xs = new xmlserializer(obj.gettype()); using(textwriter writer = new streamwriter(path, false))  {     xs.serialize(writer, obj); } 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -