views:

153

answers:

1

I want to make the calls to a method parallel. In this method I calculate some value (very fast) and write the value of this calculation into a database table.

Therefore I use a Threadpool

ThreadPool.QueueUserWorkItem((state) => {
               method();
            });

After a while of calculation (not deterministic, the time when this happens varies, sometimes after a few minutes, sometimes after some hours) the Threadpool is full, but not with the thread of method() but with the thread of my mysql class that calls MySqlCommand.ExecuteNonQuery.

Does anyone have an idea how to avoid this? In the documentation of the mysql classes I found out that you cannot terminate an executing query but how to avoid such problems?

+4  A: 

Do you need to write all values to the DB once they are calculated, or can you wait till you have some, and do a batch update where you insert a lot of them?

You could be running into a locking issue here, if all threads try to write to the same table at the same time. So try to store the numbers locally and then submit them in one big batch

Heiko Hatzfeld
ok I now add the queries to a list and send this list every minute but now I when I fill the list within my threads and after a minute send the queries, the enumeration of the list is changed within the threads, what about that? I tried lock(list) { // send the queries } but this does not work
Xelluloid
oh ... think I have had misunderstanding about the locking mechanism :) I now lock another Object when sending the list and when filling the list
Xelluloid