1

The problem is that the program throws an exception. After creating multiple threads. How can we limit the number of threads to be created in the loop?

 for (int jrCnt = rCnt; jrCnt <= arrayTable.GetUpperBound(0); jrCnt++)
                {
                    /* bla bla bla */

                    if ((!string.IsNullOrEmpty(prcI.name)) &&
                        (prcI.prc != 0))
                    {
                        /*bla bla bla*/

                        var thread = // run updade or add
                                                    new Thread(() =>
                                                    {

                                                        if (!Accessor.AddProductUpdateProduct(prcI)) _updateCounter++;
                                                        _countadd++;

                                                    });
                        thread.Name = "Add_or_update-no_" + thread.ManagedThreadId;
                        thread.Priority = ThreadPriority.Lowest;

                        thread.Start();
                    }

Some clarification.

Here is the loop starts top n number of times. Once I add Threadpool this loop is very fast. Therefore Threadpool triggered 180 times. I apologize for my English.

for (int i = sbook; i < book; i++)
            {
                dt = Accessor.ImporterXls(_path, i);// array for method

                ConstructWithBook(dt, rCnt, sbook, book, priceSelect, nametov, pricetov,
                                 categorytov);
            }
2
  • You need to provide some context here and word your question more specifically. Directly answering your question now, I'd say remove the Thread.Start.. Commented Jan 8, 2012 at 4:31
  • I want a piece of code executed in a separate thread. I need it to speed up the program. This loop handles the 9000 records. Therefore, creating a lot of threads and not enough memory. Commented Jan 8, 2012 at 4:54

2 Answers 2

1

Try using a ThreadPool, MSDN Thread Pool Class Description

Sign up to request clarification or add additional context in comments.

4 Comments

Used the threadpool. But the cycle is fast paced, and processed 180 records from 9000.
That is the source of your problem, you are exceeding the available system resources. The Thread Pool should be more efficient since you are not creating new threads. Try looking at the GetMaxThreads and SetMaxThreads Methods
I added the code that causes this problem. Thank you for helping!
Problem solved! Thank you! All threads execute as expected. My progress bar I cheated.
1

You want to use a Thread Pool: http://msdn.microsoft.com/en-us/library/h4732ks0.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.