472,958 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 3217
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...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.