开发者

PLINQ update failed

sorry for my English. So, here is my question I'm trying to update DataTable by PLINQ Here is my code

DataTable table = new DataTable();

table.Columns.Add(new DataColumn("val", typeof(decimal)));

int N = 1000000;

for (int i = 0; i < N; i++) table.Rows.Add(new object[] { i });

table.AsEnumerable().AsParallel().ForAll(row => row["val"] = 3);

But there is开发者_如何学Python exception:"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

Please, help me


Well, I can tell you right now that modifying the rows of a DataTable in parallel is not Kosher (from the MSDN documentation on the DataTable class):

This type is safe for multithreaded read operations. You must synchronize any write operations.

So while I'm not sure exactly what's causing the particular exception you mention, I know that you really shouldn't be attempting this as it is unsupported.


Found solution:

table.AsEnumerable().AsParallel().ForAll(row => { lock(table)row["val"] = 3; });

But after that - parallel makes no sense

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜