473,396 Members | 2,092 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,396 software developers and data experts.

how many threads are used by one Threading.Timer?

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.Threading;
namespace NS_ThreadingTimer
{
class TimerState
{
public int counter = 0; // thread parameter
public volatile Timer tmr; // timer reference needed for disposing
}
class App
{
public static void Main()
{
Console.WriteLine("starting timer, press ENTER ...");
Console.ReadLine();
// VisualStudio shows here one thread:
// NS_ThreadingTimer.App.Main (location)
// ProcessExplorer and TaskManager shows here 3 threads:
// mscoree.dll!_CorExeMain
// mscorwks.dll!DebuggerRCThread::ThreadProcStatic
// mscorwks.dll!Thread::intermediateThreadProc
// 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(1000);
// VisualStudio shows 2 threads:
// NS_ThreadingTimer.App.Main (location)
// System.IO.__ConsoleStream.ReadFileNative
// ProcessExplorer and TaskManager shows here 7 threads:
// mscoree.dll!_CorExeMain
// mscorwks.dll!DebuggerRCThread::ThreadProcStatic
// mscorwks.dll!Thread::intermediateThreadProc
// mscorwks.dll!ThreadpoolMgr::TimerThreadStart
// mscorwks.dll!ThreadpoolMgr::intermediateThreadProc
// mscorwks.dll!ThreadpoolMgr::intermediateThreadProc
// mscorwks.dll!ThreadpoolMgr::GateThreadStart
Console.WriteLine("Timer example done, press ENTER to continue
....");
Console.ReadLine();
}
static void CheckStatus(Object state)
{
// VisualStudio shows 2 threads:
// NS_ThreadingTimer.App.Main (location)
// NS_ThreadingTimer.App.CheckStatus (location)
// ProcessExplorer and TaskManager shows here 7 threads:
// mscoree.dll!_CorExeMain
// mscorwks.dll!DebuggerRCThread::ThreadProcStatic
// mscorwks.dll!Thread::intermediateThreadProc
// mscorwks.dll!ThreadpoolMgr::TimerThreadStart
// mscorwks.dll!ThreadpoolMgr::intermediateThreadProc
// mscorwks.dll!ThreadpoolMgr::intermediateThreadProc
// mscorwks.dll!ThreadpoolMgr::GateThreadStart
TimerState s =(TimerState) state;
s.counter++;
Console.WriteLine("{0}: {1}.", DateTime.Now.TimeOfDay,
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

Nov 17 '05 #1
3 5950


"mjheitland" <in**@dotnet-it.de> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.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.Threading;
namespace NS_ThreadingTimer
{
class TimerState
{
public int counter = 0; // thread parameter
public volatile Timer tmr; // timer reference needed for disposing
}
class App
{
public static void Main()
{
Console.WriteLine("starting timer, press ENTER ...");
Console.ReadLine();
// VisualStudio shows here one thread:
// NS_ThreadingTimer.App.Main (location)
// ProcessExplorer and TaskManager shows here 3 threads:
// mscoree.dll!_CorExeMain
// mscorwks.dll!DebuggerRCThread::ThreadProcStatic
// mscorwks.dll!Thread::intermediateThreadProc
// 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(1000);
// VisualStudio shows 2 threads:
// NS_ThreadingTimer.App.Main (location)
// System.IO.__ConsoleStream.ReadFileNative
// ProcessExplorer and TaskManager shows here 7 threads:
// mscoree.dll!_CorExeMain
// mscorwks.dll!DebuggerRCThread::ThreadProcStatic
// mscorwks.dll!Thread::intermediateThreadProc
// mscorwks.dll!ThreadpoolMgr::TimerThreadStart
// mscorwks.dll!ThreadpoolMgr::intermediateThreadProc
// mscorwks.dll!ThreadpoolMgr::intermediateThreadProc
// mscorwks.dll!ThreadpoolMgr::GateThreadStart
Console.WriteLine("Timer example done, press ENTER to continue
...");
Console.ReadLine();
}
static void CheckStatus(Object state)
{
// VisualStudio shows 2 threads:
// NS_ThreadingTimer.App.Main (location)
// NS_ThreadingTimer.App.CheckStatus (location)
// ProcessExplorer and TaskManager shows here 7 threads:
// mscoree.dll!_CorExeMain
// mscorwks.dll!DebuggerRCThread::ThreadProcStatic
// mscorwks.dll!Thread::intermediateThreadProc
// mscorwks.dll!ThreadpoolMgr::TimerThreadStart
// mscorwks.dll!ThreadpoolMgr::intermediateThreadProc
// mscorwks.dll!ThreadpoolMgr::intermediateThreadProc
// mscorwks.dll!ThreadpoolMgr::GateThreadStart
TimerState s =(TimerState) state;
s.counter++;
Console.WriteLine("{0}: {1}.", DateTime.Now.TimeOfDay,
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.
Nov 17 '05 #2
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

Nov 18 '05 #3

"mjheitland" <in**@dotnet-it.de> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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.


Nov 18 '05 #4

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

Similar topics

2
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
4
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...
22
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;
1
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...
6
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...
4
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...
1
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...
4
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...
9
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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...
0
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...
0
tracyyun
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...
0
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,...

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.