Monday, 15 September 2014

.net - Is there a way to perform parallel operation on enumerated object (IDataReader) using TPL? -



.net - Is there a way to perform parallel operation on enumerated object (IDataReader) using TPL? -

i have enumerated object using

int count=0; string key= string.empty string prv_key = string.empty while(source.movenext()) { count++; key = source["itemid"] --- ---- logic --- prv_key = key; }

can convert above code tpl thread-safe?? note : don't have count number of rows source object

your code shows there's ienumerator<t> in scope, let's assume have ienumerable<t> used create enumerator.

you can utilize parallel.foreach parallelize loop, , interlocked.increment atomically increment count.

also, you've shown, seems there's no reason declare both strings outside loop. unless need these variables shared across threads, don't.

int count = 0; parallel.foreach(items, => { int newcount = interlocked.increment(ref count); string key = source["itemid"] string prv_key = string.empty; });

.net multithreading task-parallel-library

No comments:

Post a Comment