c# 4.0 - C# parallel programming modifying xDocument -


i never tried parallel programming before in c#. so, before jumped in, wish can fast answer know if worth delving it, or not. have c# wcf web services applications .net 4.0. (it possible upgrade 4.5 if parallel programming works)

all services rest services. there 1 service in particular taking long time. service processing , modifying xml document. service accept xml string input, , give modified xml file.

the service processing xml in different locations, , different elements. so, created classes inherits interface called idocumentprocessor, , have list of those

the code briefly looks this

interface idocumentprocessor {      void process(xdocument doc); }  public class dateprocessor : idocumentprocessor {    public void process(xdocument doc) {....}; }  public class countryprocessor : idocumentprocessor {    public void process(xdocument doc) {....}; }   public class addressprocessor : idocumentprocessor {    public void process(xdocument doc) {....}; }   public class authorprocessor : idocumentprocessor {    public void process(xdocument doc) {....}; }  ....  public class documentprocessorservice {     public class processdocument(string xmlfileasstring)      {         var processorlist = new list<idocumentprocessor>{             new dateprocessor();             new countryprocessor();             new addressprocessor();             new authorprocessor();         }          var xdocument = xdocument.parse(xmlfileasstring);         processorlist.foreach(x => x.process(xdocument));     } } 

so fast question, , before delve making parallel: can parallel computing modify same xdocument object (in different locations)

and can code translated parallel computing .net 4.0?

from xelement documentation:

thread safety public static (shared in visual basic) members of type thread safe. instance members not guaranteed thread safe.

it means cant modify xdocument in parallel.

another issue not practical fork on type of operation on same data.

this way not scalable.

there might not enough operation types fork every cpu core , 1 type might finish faster other. contention high.

if document contains collection of high level elements of similar scheme, process copies in parallel , replace old ones new ones.

the reassembly operation must done in 1 thread , should not expensive if choose correct level of granularity.

you need copy constructor each xelement processed.

    var newelements = collectionelement.elements().select(el=>     process(new xelement(el))).asparallel();    var newcollection = new xelement("items", newelements); 

Comments