473,664 Members | 3,022 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_De legate dlgt = new
WorkerThread_De legate(Class2.W orkerThread);
IAsyncResult ar = dlgt.BeginInvok e(new AsyncCallback(C allback),dlgt );
}
Console.WriteLi ne("** FINISHED SPAWNING THREADS **");

(The Class2.WorkerTh read 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 3266
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.Threadin g.ThreadPool.Qu eueUserWorkItem or BeginInvoke &
QueueUserWorkIt em both wind up calling the same thing...

Hope this helps
Jay

"Mark Hoffman" <po********@hot mail.com> wrote in message
news:3pTmb.3532 4$Tr4.64310@att bi_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_De legate dlgt = new
WorkerThread_De legate(Class2.W orkerThread);
IAsyncResult ar = dlgt.BeginInvok e(new AsyncCallback(C allback),dlgt );
}
Console.WriteLi ne("** FINISHED SPAWNING THREADS **");

(The Class2.WorkerTh read 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
2154
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 worker threads. Yes I have gotten it to work, kind-of - that is I have gotten the shelled processes off of the UI thread (I think?). But the UI still is not updating properly! Still I have white boxes for forms, label text that does not update, and...
3
506
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 to the main thread to be threadsafe. I want to make this worker class I'm writing a self contained assembly so that other's can drop it into their projects. My question is: How can I NOT force those implementing my class to have to call...
5
8103
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 thread is aborted by the system. How is one to keep the background thread from calling form.Invoke after the form's window handle has been destroyed? This is definitely happening in my application. I haven't read anything about this problem in...
4
2617
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 sort of ipc will i use between threads? (i used PostThreadMessage on win32)
5
4661
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 RichEditBox. To give you some idea of the approach I took... private static RichTextBox rtb; private delegate void RtbAppendTextHandler(string output); private static RtbAppendTextHandler RtbAppendText; Log.Init(RichTextBox richTextBox,...
7
2120
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 waits for the worker thread to complete by means of a while t.isAlive, sleep(0) mechanism. But when my worker thread calls my UpdateProgressBar routine, which calls Me.Invoke, the invoke call blocks forever. But I can't figure out why the main...
7
2683
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 a lock pending, for example. I want to be able to stop a thread temporarily, and then optionally resume it or stop it for good.
5
3538
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 only one worker thread running at any time (if a new worker thread start has been requested, any running worker thread results will be invalid). I'm using the below method to invoke a new worker thread, but when stress testing this I'm sometimes...
0
1123
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 are implemented in CLI and it all goes up to C# where I use control.invoke(...) to change thread so that the GUI can be updated in the correct thread. Everything is fine. However, I also use some OpenGL stuff in C++ that intercepts the event and starts...
0
8437
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8778
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7375
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4185
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2764
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1759
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.