c# - indexoutofrangeexception in Datatable with Parallel.Foreach -


i trying augment datatable has ip addresses in 1 column reverse dns mapping. getting datatable somewhere else. exporting table sql server using sqlbulkcopy

i adding 2 columns, 1 dns name , 1 top level domain part only. since have lot of ips , doing reverse dns takes time using parallel each. strangely odd unpredictable indexoutofrangeexceptions within nestedexception parallel loop (or outside of parallel loop when call clear in datatable). why?

here doing

        //mapping specifies mapping within datatable , database          sqlbulkcopy copy = new sqlbulkcopy(cn);         foreach (keyvaluepair<int, int> pair in mapping)         {                                                                                    copy.columnmappings.add(pair.key, pair.value);         }           dt.columns.add("url");         dt.columns.add("domain");         solver.adddnsinfo(dt, 1); //second row has ip         copy.writetoserver(dt);         dt.clear();     //exceptions thrown here            //ipindex index within datatable ip of interest is.      //in scenario ipindex=1     public void adddnsinfo(datatable table, int ipindex)     {         parallel.foreach(table.asenumerable(), row =>         {                 string url = getdnsname((string)row[ipindex]);                 row["url"] = url; //exceptions thrown here                 row["domain"] = gettopleveldomain(url);                 console.write("*");         }); 

because datatable not thread safe multithreaded write operation.

msdn says:

this type safe multithreaded read operations. must synchronize write operations.

you writing new columns multiple threads @ same time when, in parallel.foreach, :

  row["url"] = url;   row["domain"] = gettopleveldomain(url); 

you need synchronize writing call datatable (using lock, or form of monitor)


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -