Hi,
I like to know how many threads are used by a Threading.Timer object.
When I create a Threading.Timer object calling a short running method
every 5 seconds I expected to have one additional ThreadPool thread.
And that is exactly what MS VIsual Studio shows.
But when I run Processexplorer or Taskmanager I see 2 additional
threads, after a while another 2 additional threads. With the 3 threads
at start time we have totally 7 threads.
Please follow the code below to get the locations of the threads.
Does anyone know
1. Where do the three start threads come from (UI thread, GC?, and ?)
2. Does the timer itself consumes permanently a thread? Not only the
method he invokes? This would explain why we get two additional
threads.
If yes, is it a ThreadPool thread?
3. Why do we see another pair of additional threads?
4. Is it dangerous to use many Threading.Timer objects (even for
shortrunning methods)=
// program to demonstrate thread usage by Threading.Timer
using System;
using System.Threadin g;
namespace NS_ThreadingTim er
{
class TimerState
{
public int counter = 0; // thread parameter
public volatile Timer tmr; // timer reference needed for disposing
}
class App
{
public static void Main()
{
Console.WriteLi ne("starting timer, press ENTER ...");
Console.ReadLin e();
// VisualStudio shows here one thread:
// NS_ThreadingTim er.App.Main (location)
// ProcessExplorer and TaskManager shows here 3 threads:
// mscoree.dll!_Co rExeMain
// mscorwks.dll!De buggerRCThread: :ThreadProcStat ic
// mscorwks.dll!Th read::intermedi ateThreadProc
// start timer immediately, then invoke CheckStatus every 5
seconds.
TimerState s = new TimerState();
TimerCallback timerDelegate = new TimerCallback (CheckStatus);
Timer timer = new Timer (timerDelegate, s, 0, 5000);
s.tmr = timer; // Keep handle to timer, so it can be disposed.
while (s.tmr != null) // Main thread NOP until timer is disposed.
Thread.Sleep(10 00);
// VisualStudio shows 2 threads:
// NS_ThreadingTim er.App.Main (location)
// System.IO.__Con soleStream.Read FileNative
// ProcessExplorer and TaskManager shows here 7 threads:
// mscoree.dll!_Co rExeMain
// mscorwks.dll!De buggerRCThread: :ThreadProcStat ic
// mscorwks.dll!Th read::intermedi ateThreadProc
// mscorwks.dll!Th readpoolMgr::Ti merThreadStart
// mscorwks.dll!Th readpoolMgr::in termediateThrea dProc
// mscorwks.dll!Th readpoolMgr::in termediateThrea dProc
// mscorwks.dll!Th readpoolMgr::Ga teThreadStart
Console.WriteLi ne("Timer example done, press ENTER to continue
....");
Console.ReadLin e();
}
static void CheckStatus(Obj ect state)
{
// VisualStudio shows 2 threads:
// NS_ThreadingTim er.App.Main (location)
// NS_ThreadingTim er.App.CheckSta tus (location)
// ProcessExplorer and TaskManager shows here 7 threads:
// mscoree.dll!_Co rExeMain
// mscorwks.dll!De buggerRCThread: :ThreadProcStat ic
// mscorwks.dll!Th read::intermedi ateThreadProc
// mscorwks.dll!Th readpoolMgr::Ti merThreadStart
// mscorwks.dll!Th readpoolMgr::in termediateThrea dProc
// mscorwks.dll!Th readpoolMgr::in termediateThrea dProc
// mscorwks.dll!Th readpoolMgr::Ga teThreadStart
TimerState s =(TimerState) state;
s.counter++;
Console.WriteLi ne("{0}: {1}.", DateTime.Now.Ti meOfDay,
s.counter);
if (s.counter == 10)
{
s.tmr.Dispose() ;
s.tmr = null;
}
}
}
}
So many questions, but I could not find the answer out of the docs.
Greetings,
Michael Heitland 3 5972
"mjheitland " <in**@dotnet-it.de> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. . Hi,
I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional ThreadPool thread. And that is exactly what MS VIsual Studio shows.
But when I run Processexplorer or Taskmanager I see 2 additional threads, after a while another 2 additional threads. With the 3 threads at start time we have totally 7 threads. Please follow the code below to get the locations of the threads. Does anyone know
1. Where do the three start threads come from (UI thread, GC?, and ?)
2. Does the timer itself consumes permanently a thread? Not only the method he invokes? This would explain why we get two additional threads. If yes, is it a ThreadPool thread?
3. Why do we see another pair of additional threads?
4. Is it dangerous to use many Threading.Timer objects (even for shortrunning methods)=
// program to demonstrate thread usage by Threading.Timer using System; using System.Threadin g; namespace NS_ThreadingTim er { class TimerState { public int counter = 0; // thread parameter public volatile Timer tmr; // timer reference needed for disposing } class App { public static void Main() { Console.WriteLi ne("starting timer, press ENTER ..."); Console.ReadLin e(); // VisualStudio shows here one thread: // NS_ThreadingTim er.App.Main (location) // ProcessExplorer and TaskManager shows here 3 threads: // mscoree.dll!_Co rExeMain // mscorwks.dll!De buggerRCThread: :ThreadProcStat ic // mscorwks.dll!Th read::intermedi ateThreadProc // start timer immediately, then invoke CheckStatus every 5 seconds. TimerState s = new TimerState(); TimerCallback timerDelegate = new TimerCallback (CheckStatus); Timer timer = new Timer (timerDelegate, s, 0, 5000); s.tmr = timer; // Keep handle to timer, so it can be disposed. while (s.tmr != null) // Main thread NOP until timer is disposed. Thread.Sleep(10 00); // VisualStudio shows 2 threads: // NS_ThreadingTim er.App.Main (location) // System.IO.__Con soleStream.Read FileNative // ProcessExplorer and TaskManager shows here 7 threads: // mscoree.dll!_Co rExeMain // mscorwks.dll!De buggerRCThread: :ThreadProcStat ic // mscorwks.dll!Th read::intermedi ateThreadProc // mscorwks.dll!Th readpoolMgr::Ti merThreadStart // mscorwks.dll!Th readpoolMgr::in termediateThrea dProc // mscorwks.dll!Th readpoolMgr::in termediateThrea dProc // mscorwks.dll!Th readpoolMgr::Ga teThreadStart Console.WriteLi ne("Timer example done, press ENTER to continue ..."); Console.ReadLin e(); } static void CheckStatus(Obj ect state) { // VisualStudio shows 2 threads: // NS_ThreadingTim er.App.Main (location) // NS_ThreadingTim er.App.CheckSta tus (location) // ProcessExplorer and TaskManager shows here 7 threads: // mscoree.dll!_Co rExeMain // mscorwks.dll!De buggerRCThread: :ThreadProcStat ic // mscorwks.dll!Th read::intermedi ateThreadProc // mscorwks.dll!Th readpoolMgr::Ti merThreadStart // mscorwks.dll!Th readpoolMgr::in termediateThrea dProc // mscorwks.dll!Th readpoolMgr::in termediateThrea dProc // mscorwks.dll!Th readpoolMgr::Ga teThreadStart TimerState s =(TimerState) state; s.counter++; Console.WriteLi ne("{0}: {1}.", DateTime.Now.Ti meOfDay, s.counter); if (s.counter == 10) { s.tmr.Dispose() ; s.tmr = null; } } } }
So many questions, but I could not find the answer out of the docs.
Greetings, Michael Heitland
What taskman shows you are the OS threads, the two additional threads are
the "Debug helper" and the "Finalizer" thread.
So, a process that loads the CLR always starts with a minimum of 3 threads,
a main thread + the two above mentioned addtional threads. Whenever you
start using Timers or Asynch callbacks, async IO etc..., the CLR will
request the threadpool manager to create some additional threads, the
minimum of initial threads created by the TPM is two plus one watchdog
(gate) thread.
So the picture for your sample looks like :
1. Main thread.
2. Debug helper thread (used by managed debuggers, but always present).
3. Finalizer thread.
4, 5 Threadpool threads
- IOC thread (your timer)
- Worker thread (idle)
6 Threapool manager - Gate
Willy.
Hi Willy,
thanx for your quick reply.
Now it became clearer to me, but some questions are still left:
1. what is the IOC thread (IO control?)?
2. Is the IOC thread a threadpool thread? May I run into resource
problems, if I use more than 25 Threading.Timer objects (the threadpool
is limited to 25 threads per processor in .NET 1.1)?
3. Why do I see another pair of threads after a while?
4. Is the gate thread also a ThreadPool thread?
Michael
"mjheitland " <in**@dotnet-it.de> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. . Hi Willy,
thanx for your quick reply.
Now it became clearer to me, but some questions are still left: 1. what is the IOC thread (IO control?)? 2. Is the IOC thread a threadpool thread? May I run into resource problems, if I use more than 25 Threading.Timer objects (the threadpool is limited to 25 threads per processor in .NET 1.1)? 3. Why do I see another pair of threads after a while? 4. Is the gate thread also a ThreadPool thread?
Michael
1. IOC - (IO) Completion also called a "Completion Port".
2. Yes, there are two distinct Threadpools - A worker threadpool and a CP
threadpool.
- the first has a per process per CPU limit of 25 (default)
- the second has a per process limit of 1000 (default)
You may run into resource problems when you have a lot of timers that have
their handler in parallel for an extended period of time. So you should keep
the time spent in the handler as short as possible, or you should start an
auxiliary thread to handle long running tasks from timer events.
3 If a timer fires the CLR asks the Threadpool to create a worker and a CP
thread.
- the CP thread (only oner per process) manages the timer queue
- the worker runs the timer event-handler
4. No the "gate" (only one per process) is kind of watchdog thread used by
the threadpool manager to monitor the status of the workers and CP's.
Note that all this is internal stuff and may vary with the CLR
implementation and version.
Willy. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: WinstonSmith |
last post by:
Hallo everyone,
is there a way to get a list of all threads an application written in C#
uses?
Thanks in advance
Matthias
|
by: Trev Hunter |
last post by:
Hi All,
I was just wondering if it is safe to change thread priorities with thread
pool threads (threads used by System.Threading.Timer and those used by the
ThreadPool class itself).
Say I have a background task that runs periodically (it uses
System.Threading.Timer to do this). As the execution speed of this task is
not a priority, I want to be able to set the priority of the current thread
to the lowest possible. If I use
|
by: Jeff Louie |
last post by:
Well I wonder if my old brain can handle threading. Dose this code look
reasonable.
Regards,
Jeff
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
|
by: Chuck |
last post by:
I am creating a pub/sub broker implementation in C# and I am having some
trouble understanding how I should best implement the invoke portion in
order to make sure it is as thread-safe as it can be.
The "broker" class allows other objects to subscribe to messages that will
get published by other objects. To subscribe they pass in a Object that
represents thier instance (aka 'this') and a String that contains the method
name to be...
|
by: RahimAsif |
last post by:
Hi guys,
I would like some advice on thread programming using C#.
I am writing an application that communicates with a panel over
ethernet, collects data and writes it to a file. The way the data is
collected is that we have different schedules (so one set of data is
collected say every second, another set of data might be collected
every 30 seconds, and so on).
| |
by: Manuel |
last post by:
I have a long function that needs to be done 1000 times. I'm
multithreading it, but I don't want to load them up all at once, instead
load them 10 at a time.
So far the only way I can get it to work is by creating a dummy form
with a timer. On the timer function I test if the number of threads are
less than 10 then run the remaining ones. This is working fine, but I
would like to know how to do it without the form.
This is the code...
|
by: ChainsawDude |
last post by:
I note that when I set a culture in the globalization section of the
web.config this is picked up and used for methods such as ToString() for
dates etc.
However, it is not picked up for timers which I create in my web service and
the culture (but not uiCulture) revert to the system default!
Does anyone know if this is a bug and how to get around it?
asp.net 1.1 web service
|
by: gsimmons |
last post by:
I've been researching multi-threaded WinForms apps and thread
synchronization stuff for a couple days since I'm working on
refactoring a multi-threaded GUI app at work and want to be sure it's
rock solid/thread-safe. I've seen all the posts about using BeginInvoke
to have worker threads interact with the UI. My question is this:
I created a plain old Windows Form application (VS.NET 2005) with a
blank form, built it (release build), ran...
|
by: koschwitz |
last post by:
Hi,
I hope you guys can help me make this simple application work. I'm
trying to create a form displaying 3 circles, which independently
change colors 3 times after a random time period has passed.
I'm struggling with making the delegate/invoke thing work, as I know
GUI objects aren't thread-safe. I don't quite understand the concept
I'm supposed to use to modify the GUI thread-safe.
Below is my form and my Circle class. Currently,...
|
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...
|
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...
| |
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,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |