I have a class that creates worker threads using Asnychronous Method Calls using an Array of Delegates each one is invoked by iterating through the array and calling BeginInvoke (have posted an excerpt below *not the actual code*). My question is what is the suggested maximum number of Asynchronous items to have working in the background at the same time. I know each is a new thread and by default there is a maximum of 25 threads available in the pool on a normal single processor workstations. I also know that when the maximum is reached the additional ones are serialized and put into a holding area (qued ) until another thread becomes available on the Thread Pool.
What I dont know is what the best practice is on how many to have running at once and I know this can be based on circumstances but I am looking for a "in general" answer. Right now I drop the items to be worked into a stack, I run 5 Asynchronous methods at the same time, when one is done it checks the stack and starts to process another item. So in essence I have 5 threads popping stuff off the stack as they are worked.
Is this the best method Am I doing this totally backwards Thanks for your advice and info I really appreciate it!!
Code Excerpt

Asynchronous Method Calls - Whats the max?
deepforest
Hi Eric,
Thanks for your feedback and cheers for your excellent job in solving the problem.
I've noticed your new problem.
Would you post your new problem in another post with corresponding topic Therefore, more help will be given to solve it.
Many thanks for your cooperation.
Best regards,
Cleo
Adrien Barr
Hi Eric,
How does your project go Have you resolved your problem
Looking forward to your further improvement.
Best regards,
Cleo
Deefken
Hi Eric,
So nice to receive those creative ideas from you. I have some points for you:
1. Threads number should be controlled as few as possible. First, if you only have one CPU on your computer, the total performance counts on all processes. So if your application takes up one process, it only can be said one of the aspects to influence the performance while the total depends on how many threads of all processes. Second, as what I mentioned before, in one process, try to reduce the number of threads, 'coz the more threads, the more switch and synchronous cost will be spent.
2. There is one thread pool per process. The thread pool has a default size of 25 threads per available processor. In your scenario, you have 5 threads controlled by yourself in a stack. And I suggest if your number of threads is within 25, supposing 6, while your number of CPU is within 6, just throw them into the threadpool and let the processor to deal with it. Never run 5 threads first and check the stack to run the next one by hand, because the scheduler of CPU will be much smarter than your knowledge about existing threads. And if you put all the 6 threads into the process threadpool, they will be given the even probability to share the processor. Each thread uses the default stack size and runs at the default priority. However, if the thread number exceeds 25, supposing 26 in one process, there must be one thread suspending until one of the other 25 completes. Also you can use SetMaxThread method to set the maximum number of threads in one process. For example, if you have 6 threads in total, you can just set the threadpool max size to 6, which enables a higher intelligence of CPU.
Thanks and sincere hope you can get something from it. Please feel free to ask if any problems.
Best regards,
Cleo
Tim Burton
Hi
Congratulations on your excellent solution. And please mark the post as answered if the problem has been solved. Many thanks! .
Regards,
Cleo
Meatnog
Hi Eric,
Thanks for your questions about multithreading in .Net Framework. The intial idea about multithreading just concerns about performance of code and system.
Based on my understanding, what you cares about the most is the best performance of multithread programming when you use .net framework to implement it.
First, here come some guidelines of threading design:
Second, I'm afraid it can be generally said what's the perfect number of thread number, as what you mentioned, it depends on circumstances. But I have found out some good pratices for you to improve your multithread programming:
Thread Execution Management and Advanced Thread Queuing in .NET 1.x and 2.0
http://www.codeproject.com/csharp/AdvancedThreadQueuing.asp
Here are also some tests about ThreadPool class behind the scene and strongly suggested to use ManagedThreadPool for better performance.
http://www.codeproject.com/csharp/threadtests.asp
Just remember that although performance level is boosted by creating multiple threads, yet thread creation is an expensive proposition in terms of the memory required and CPU time consumed to keep them running. Stay away from creating too many useless threads and assigning incorrect priorities to them.
Sincere hope it can be of any help to you. Please feel free if any questions.
Best regards,
Cleo
bobchen
Thanks Cleo, unfortunately I am very familiar with both the articles you mentioned and have gone through them and tested it all out myself. Also I am not using Threads I am using Asynchronous Delegate Calls to perform everything, this way .NET will handle the Thread Pooling and management of the threads and I do not have to rely on a seperate ThreadManagement class to take care of all of that, but I was just wondering if there were guidelines or best practices. I guess from your response the answer is its up to the developer. In my case I have deemed 5 Ansynchronous Delegate Calls can be run Sumultaneously but no more than that, thats the reason I created the stack. If I were to Batch process everything, from testing .NET would call nearly 400 Ansynchronous Delegates, the majority of which are then serialized and put into a waiting state for free threads on the Thread Pool, this took up about 600 MB of memory since all those classes were being serialized in memory and put into a holding state. Not only that but the entire pool was depleted for .NET since I was using all of them so not the best way to go about it.
One other question I have for you is are the Thread Pools shared across Application Domains Or instead is their a dedicated Thread Pool for each application domain
Thanks,
Eric
jwize
I have resolved the delegates issue and it is working perfectly. The problem I have now is how do you stop an asynchrnous method call while its running EndInvoke requires a IAsyncResult object to be passed to it, I do not have an IAsyncResult object since I want to end the call by clicking a "Stop" button.
Another issue I have is when you subscribe to events in the class, these events are running under the Threads process, so the events that are subscribed to the form's class cant update the forms controls without using delegates. That works fine but when the form is closed the Thread is still loading which causes an exception to be thrown since the form handles are nulling out and the asynchronous thread is trying to access those, to solve this I unsubscribe from the events during the Form "Closing" event. Which seemed to have fixed the issue, I have to check if the delegate is still subscribed in the events but it appears to be working.
It all works but I am not sure if thats the right way to do Even Subscribing across thread processes, is their a good article about event subscribing across threads that you know of
Thanks!!