c# - how to dispose all entity created in parallel.forEach after job done? -
c# - how to dispose all entity created in parallel.forEach after job done? -
i'm using parallel.foreach access database multi threads.
let's 200,000 rows insert, , i'm using 20 threads
optimistically, need connect db 20 time, each thread utilize 1 connection.
i think if write dbconn.dispose()
within loop 200,000 connection.
but can not write dbconn.dispose()
outside , no defined.
then how can write thing " dispose connection after 200,000 rows done"?
code like:
mypallopt.maxdegreeofparallelism=20; parallel.foreach(source,mypallopt,(onerow)=> { mydataentity dbconn= new mydataentity(); // info base of operations job // don't write dbconn.dispose() here }); //but can not utilize dbconn.dispose() here
you want utilize overload of parallel.foreach
allows setup , teardown thread. http://msdn.microsoft.com/en-us/library/dd991486(v=vs.110).aspx has more details , example.
for scenario..
parallel.foreach( input, () => new dbconn(), (i, loopstate, dbconn) => { // stuff dbconn here //pass dbconn on next itteration. homecoming dbconn; }, dbconn => dbconn.dispose() );
c# multithreading entity-framework
Comments
Post a Comment