473,383 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Max # of worker threads and Begin Invoke

All,

From what I've read, the CLR gives each App Domain a thread pool of 25
threads, and once this pool is exhausted then any new threads created with
BeginInvoke will block until the pool frees up another thread. Am I right on
that?

I did a little test where I went into a loop and attempted to spawn 50 new
worker threads with a call to BeginInvoke that used an asynchronous
callback. I expected it to launch 24 threads, then block for a while until
the threads completed. But instead, it never blocked and completed the loop
immediately. Here is a snippet of the code:

for (int i=0;i<50;i++)
{
WorkerThread_Delegate dlgt = new
WorkerThread_Delegate(Class2.WorkerThread);
IAsyncResult ar = dlgt.BeginInvoke(new AsyncCallback(Callback),dlgt );
}
Console.WriteLine("** FINISHED SPAWNING THREADS **");

(The Class2.WorkerThread simply causes the thread to sleep for 10 seconds.)

So when I run this, it immediately finishes the loop and outputs the line
about finished spawning threads. Why didn't it block after creating the
first 24 threads? Is it because I am using an asynchronous callback?

Thanks for any info. I've read what I've seen on MSDN, but either I missed
something or it just isn't sinking in because I don't fully understand this
behavior.

Mark
Nov 15 '05 #1
1 3246
Mark,
The way I understand it BeginInvoke, places the request in a queue and
immediately returns. The 25 worker threads check this queue for the next
item to work on and start executing that request.

So what happened is you have 25 threads doing working and 25 items waiting
in a queue someplace. As the first 25 items are finished, they (the threads)
will take items out of the queue and process those requests.

Hence your loop finishes right away.

I suspect someplace under the covers BeginInvoke simply uses
System.Threading.ThreadPool.QueueUserWorkItem or BeginInvoke &
QueueUserWorkItem both wind up calling the same thing...

Hope this helps
Jay

"Mark Hoffman" <po********@hotmail.com> wrote in message
news:3pTmb.35324$Tr4.64310@attbi_s03...
All,

From what I've read, the CLR gives each App Domain a thread pool of 25
threads, and once this pool is exhausted then any new threads created with
BeginInvoke will block until the pool frees up another thread. Am I right on that?

I did a little test where I went into a loop and attempted to spawn 50 new
worker threads with a call to BeginInvoke that used an asynchronous
callback. I expected it to launch 24 threads, then block for a while until
the threads completed. But instead, it never blocked and completed the loop immediately. Here is a snippet of the code:

for (int i=0;i<50;i++)
{
WorkerThread_Delegate dlgt = new
WorkerThread_Delegate(Class2.WorkerThread);
IAsyncResult ar = dlgt.BeginInvoke(new AsyncCallback(Callback),dlgt );
}
Console.WriteLine("** FINISHED SPAWNING THREADS **");

(The Class2.WorkerThread simply causes the thread to sleep for 10 seconds.)
So when I run this, it immediately finishes the loop and outputs the line
about finished spawning threads. Why didn't it block after creating the
first 24 threads? Is it because I am using an asynchronous callback?

Thanks for any info. I've read what I've seen on MSDN, but either I missed
something or it just isn't sinking in because I don't fully understand this behavior.

Mark

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Joey Powell | last post by:
Re: Original post = Windows forms - how do I get them to render/update properly? from August 22. Okay I am making some progress with being able to use delegates to run my shelled processes on...
3
by: Jacob | last post by:
I'm working on a class that needs to be called from a windows form, do it's work, and then, show progress back to the main form. I'm well aware that worker threads need to call Invoke for updates...
5
by: Stephen Lamb | last post by:
I have a background worker thread which I start from a form's HandleCreated event that makes calls back to the form using Invoke. During shutdown the form is disposed and the background worker...
4
by: Brian Keating EI9FXB | last post by:
Hello there, Just a few questions re: worker theads. I have a worker thread that i wish to perform certain task. This worker thread must also handle events. Now my real questions..... What...
5
by: User N | last post by:
I have a log class with static methods that grab and release a mutex as required. The log class is designed to be called from both GUI and thread pool threads, and dump output to a logfile and a...
7
by: Jeff Stewart | last post by:
I need a thread to run a subroutine which updates my main form's progress bar. I've properly marshaled all UI updates to the main UI thread, and after the main thread starts the worker thread, it...
7
by: Charles Law | last post by:
My first thought was to call WorkerThread.Suspend but the help cautions against this (for good reason) because the caller has no control over where the thread actually stops, and it might have...
5
by: Soren S. Jorgensen | last post by:
Hi, In my app I've got a worker thread (background) doing some calculations based upon user input. A new worker thread might be invoked before the previous worker thread has ended, and I wan't...
0
by: Stonepony | last post by:
Hi you all, I'm writing an application where I have a C# GUI. The applicaiton can start a lot of worker threads in unmanaged C++. To get progress events the worker threads calles methods that...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.