c# - index out of range exception when using tasks -


i getting "index out of range" exception when using async tasks on array list. checked on stackoverflow , try different ways none of them working. maybe missing something... can please suggest.

        task[] datatasks = (from eachrow in ppcrowscollection.rows                             select createdatarows(eachrow, subctablearray, rowsarraywithdefaultandcommonvalues)).toarray();  private async task<datarow[]> createdatarows(ppcrow givenppcrow, datatable[] subctablearray, datarow[] rowsarraywithdefaultandcommonvalues) {     return await task.run(() =>     {         datatable[] subctablearraylocal = subctablearray;         datarow[] defaultrowsarray = getrowsarraywithdefaultandcommonvalues(ref subctablearraylocal, ref rowsarraywithdefaultandcommonvalues);         setotherthancommoncolumnvalues(ref defaultrowsarray, givenppcrow);         return defaultrowsarray;     }     ).configureawait(false); }  private datarow[] getrowsarraywithdefaultandcommonvalues(ref datatable[] subctablearray, ref datarow[] rowsarraywithdefaultandcommonvalues) {     datarow[] defaultrowsarray = subctablearray.select(x => x.newrow()).toarray(); //this getting exception      datarow[] defaultrowsarraycopy = defaultrowsarray;      foreach (datarow row in defaultrowsarraycopy)     {         row.itemarray = rowsarraywithdefaultandcommonvalues                             .where(x => x.table.tablename == row.table.tablename)                             .first()                             .itemarray;     }     return defaultrowsarraycopy; } 

stack trace:

at system.throwhelper.throwargumentoutofrangeexception() @ system.collections.generic.list1.get_item(int32 index) @ system.data.recordmanager.newrecordbase() @ system.data.datatable.newrecord(int32 sourcerecord) @ system.data.datatable.newrow(int32 record) @ system.data.datatable.newrow() @ pedwebservice.<getrowsarraywithdefaultandcommonvalues>b__21(datatable x) in c:\work\src\pedwebservice.cs:line 2913 @ system.linq.enumerable.whereselectarrayiterator2.movenext() @ system.linq.buffer1..ctor(ienumerable1 source) @ system.linq.enumerable.toarray[tsource](ienumerable`1 source) @ pedwebservice.getrowsarraywithdefaultandcommonvalues(datatable[]& subctablearray, datarow[]& rowsarraywithdefaultandcommonvalues) in c:\work\src\pedwebservice.cs:line 2913

you're calling datatable.newrow() on same table in parallel. datatable not thread-safe, why doesn't work.

if you're stuck datatable, think best option not use parallelization @ all.

you try locking problematic operation (calling newrow()), i'm not sure enough make code thread-safe.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -