468,101 Members | 1,316 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,101 developers. It's quick & easy.

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 2912
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Joey Powell | last post: by
3 posts views Thread by Jacob | last post: by
5 posts views Thread by Stephen Lamb | last post: by
4 posts views Thread by Brian Keating EI9FXB | last post: by
7 posts views Thread by Jeff Stewart | last post: by
7 posts views Thread by Charles Law | last post: by
5 posts views Thread by Soren S. Jorgensen | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.