Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle database operations using threadpool and entity framework?

I really need some code examples here... What I am trying to do:

GetCollectionFromDatabase
foreach item
{
    resetEvents[i] = new ManualResetEvent(false);
    var makeRequest = new MakeRequest(resetEvents[i]);

    ThreadPool.QueueUserWorkItem(new WaitCallback(makeRequest.ThreadPoolCallback), i);
}
db.Save();

ThreadPoolCallback    
{
    update/delete the row 
}

Is this the right appoach? Do I pass in the db as a ref to the ThreadPoolCallBack?

like image 496
Mike Ribeiro Avatar asked Jun 18 '26 03:06

Mike Ribeiro


1 Answers

You will have to create a context within the thread pool callback. Entity framework contexts are not multi threaded and cannot be shared by different threads.

I take it db is your database context in this case. You will have to create a new one in ThreadPoolCallback and save it there.

like image 75
Pieter van Ginkel Avatar answered Jun 20 '26 16:06

Pieter van Ginkel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!