Thead APIs

  • Thread starter Thread starter None
  • Start date Start date
N

None

So the .NET Thread class has a name property but the ProcessThread class
does not. And the ProcessThread class has an id property but the .NET
Thread class does not. That's just strange. You can get the thread id from
AppDomain.GetCurrentThreadId() but it seems logical that it would be
accessible through the Thread class.

I created a bare-bones Console C# app. and listed the process threads using
GetCurrentProcess().Threads. There were like 5 or 7 threads created! What
are all those threads for?
 
None said:
So the .NET Thread class has a name property but the ProcessThread class
does not. And the ProcessThread class has an id property but the .NET
Thread class does not. That's just strange. You can get the thread id from
AppDomain.GetCurrentThreadId() but it seems logical that it would be
accessible through the Thread class.

Well, a .NET thread doesn't *have* to be a process thread. For
instance, when running in Yukon, I believe threads may well end up
being mapped to fibers rather than "real" threads.
I created a bare-bones Console C# app. and listed the process threads using
GetCurrentProcess().Threads. There were like 5 or 7 threads created! What
are all those threads for?

I wouldn't like to say what all of them are for, but I'd guess that one
would be a finalization thread, and there may be one controlling the
ThreadPool, even if you haven't used the pool explicitly in your
application.
 
None said:
I created a bare-bones Console C# app. and listed the process threads
using GetCurrentProcess().Threads. There were like 5 or 7 threads
created! What are all those threads for?

Normaly a managed app only has 2 threads:
1. Main-Thread
2. GC-Finilizer-thread

If you debug your app you will also have one debugging-helper-thread.

And if your broke into you app you will have an "DbgUiRemoteBreakin"
thread.

So for normal apps you will have up to 4 threads...


--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server ?
http://sourceforge.net/projects/srvreport/
 
Well this console app. code produces EIGHT threads!

Id: 1904 State: Running
Id: 1048 State: Wait WaitReason: UserRequest
Id: 2852 State: Wait WaitReason: UserRequest
Id: 2596 State: Wait WaitReason: UserRequest
Id: 2676 State: Wait WaitReason: UserRequest
Id: 2740 State: Wait WaitReason: EventPairLow
Id: 2792 State: Wait WaitReason: UserRequest
Id: 2860 State: Ready

using System;

using System.Diagnostics;

namespace ThreadListTest

{

class Class1

{

[STAThread]

static void Main(string[] args)

{

Process process = Process.GetCurrentProcess();

foreach (ProcessThread thread in process.Threads)

{

if (thread.ThreadState == ThreadState.Wait)

Console.WriteLine("Id: " + thread.Id + " State: " +
thread.ThreadState.ToString() + " WaitReason: " +
thread.WaitReason.ToString());

else

Console.WriteLine("Id: " + thread.Id + " State: " +
thread.ThreadState.ToString());

}

}

}

}
 
None said:
Well this console app. code produces EIGHT threads!

Id: 1904 State: Running
Id: 1048 State: Wait WaitReason: UserRequest
Id: 2852 State: Wait WaitReason: UserRequest
Id: 2596 State: Wait WaitReason: UserRequest
Id: 2676 State: Wait WaitReason: UserRequest
Id: 2740 State: Wait WaitReason: EventPairLow
Id: 2792 State: Wait WaitReason: UserRequest
Id: 2860 State: Ready

Yes, but this has nothing to do with .NET... or CLR...

If you take a deeper look into the threads you will see that only 2
threads are CLR-threads (main-thread and GC-Finilizer-thread).

All other threads are from using the Perfomance-Functions to query the
Processes...
See following dump:

0:000> ~*e k
ChildEBP RetAddr
WARNING: Frame IP not in any known module. Following frames may be wrong.
0012f6a8 791da717 0x6d4032b
0012f6b0 791da434 mscorwks!CallDescrWorker+0x30
0012f7c0 791da58a mscorwks!MethodDesc::CallDescr+0x1b6
0012f870 791da5f6 mscorwks!MethodDesc::CallDescr+0x43
0012f898 7923d587 mscorwks!MethodDesc::Call+0x97
0012f950 7923d342 mscorwks!ClassLoader::CanAccess+0x1d6
0012fa64 7923d441 mscorwks!ClassLoader::ExecuteMainMethod+0x49d
0012fa7c 7923d92f mscorwks!Assembly::ExecuteMainMethod+0x21
0012fd60 791c6e73 mscorwks!SystemDomain::ExecuteMainMethod+0x416
0012ffa0 791c6ef3 mscorwks!ExecuteEXE+0x1c0
0012ffb0 7917d0b8 mscorwks!_CorExeMain+0x59
0012ffc0 77e614c7 mscoree!_CorExeMain+0x30
0012fff0 00000000 KERNEL32!BaseProcessStart+0x23

ChildEBP RetAddr
06befee8 77f4c534 SharedUserData!SystemCallStub+0x4
06befeec 77e5a62d ntdll!NtWaitForSingleObject+0xc
06beff50 77e5ac21 KERNEL32!WaitForSingleObjectEx+0xa8
06beff60 791bbe7a KERNEL32!WaitForSingleObject+0xf
06beff70 791bbea4 mscorwks!WaitForFinalizerEvent+0xde
06beffb4 77e5d33b mscorwks!GCHeap::FinalizerThreadStart+0x96
06beffec 00000000 KERNEL32!BaseThreadStart+0x37

ChildEBP RetAddr
04a4fe7c 77f4c524 SharedUserData!SystemCallStub+0x4
04a4fe80 77e55ee0 ntdll!NtWaitForMultipleObjects+0xc
04a4ff1c 77e55faa KERNEL32!WaitForMultipleObjectsEx+0x12c
04a4ff34 791d25d5 KERNEL32!WaitForMultipleObjects+0x17
04a4ff9c 791d254c mscorwks!DebuggerRCThread::MainLoop+0x90
04a4ffac 791d4d50 mscorwks!DebuggerRCThread::ThreadProc+0x68
04a4ffb4 77e5d33b mscorwks!DebuggerRCThread::ThreadProcStatic+0xb
04a4ffec 00000000 KERNEL32!BaseThreadStart+0x37

ChildEBP RetAddr
06e7ff7c 77f4c534 SharedUserData!SystemCallStub+0x4
06e7ff80 77dc5e53 ntdll!NtWaitForSingleObject+0xc
06e7ffb4 77e5d33b ADVAPI32!PerflibTimerFunction+0x8b
06e7ffec 00000000 KERNEL32!BaseThreadStart+0x37

ChildEBP RetAddr
070bfea8 77f4c524 SharedUserData!SystemCallStub+0x4
070bfeac 77e55ee0 ntdll!NtWaitForMultipleObjects+0xc
070bff48 77e55faa KERNEL32!WaitForMultipleObjectsEx+0x12c
070bff60 7976cece KERNEL32!WaitForMultipleObjects+0x17
070bffb4 77e5d33b CorperfmonExt!HandlerAuxThreadProc+0x38
070bffec 00000000 KERNEL32!BaseThreadStart+0x37

ChildEBP RetAddr
0735fee0 77f4c534 SharedUserData!SystemCallStub+0x4
0735fee4 77e5a62d ntdll!NtWaitForSingleObject+0xc
0735ff48 77e5ac21 KERNEL32!WaitForSingleObjectEx+0xa8
0735ff58 79e78e40 KERNEL32!WaitForSingleObject+0xf
0735ffac 79e78126 aspnet_isapi!CPerfCounterClient::GatherPerfData+0x207
0735ffb4 77e5d33b aspnet_isapi!PerfDataGatherThreadStart+0xa
0735ffec 00000000 KERNEL32!BaseThreadStart+0x37

ChildEBP RetAddr
0725ff14 77f4c534 SharedUserData!SystemCallStub+0x4
0725ff18 77e5a62d ntdll!NtWaitForSingleObject+0xc
0725ff7c 77e5ac21 KERNEL32!WaitForSingleObjectEx+0xa8
0725ff8c 79e789ff KERNEL32!WaitForSingleObject+0xf
0725ffac 79e78113 aspnet_isapi!CPerfCounterClient::MonitorPerfPipeNames+
0x6b
0725ffb4 77e5d33b aspnet_isapi!RegistryMonitorThreadStart+0xa
0725ffec 00000000 KERNEL32!BaseThreadStart+0x37

ChildEBP RetAddr
0792fea4 77f4c524 SharedUserData!SystemCallStub+0x4
0792fea8 77e55ee0 ntdll!NtWaitForMultipleObjects+0xc
0792ff44 77da87a4 KERNEL32!WaitForMultipleObjectsEx+0x12c
0792ffb4 77e5d33b ADVAPI32!WmipEventPump+0x22e
0792ffec 00000000 KERNEL32!BaseThreadStart+0x37

ChildEBP RetAddr
07a2ff3c 77f4c534 SharedUserData!SystemCallStub+0x4
07a2ff40 77e5a62d ntdll!NtWaitForSingleObject+0xc
07a2ffa4 7350694c KERNEL32!WaitForSingleObjectEx+0xa8
07a2ffb4 77e5d33b TRAFFIC!GpcNotifyThreadFunction+0x15
07a2ffec 00000000 KERNEL32!BaseThreadStart+0x37

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server ?
http://sourceforge.net/projects/srvreport/
 
How did you get that dump of the threads?

Jochen Kalmbach said:
Yes, but this has nothing to do with .NET... or CLR...

If you take a deeper look into the threads you will see that only 2
threads are CLR-threads (main-thread and GC-Finilizer-thread).

All other threads are from using the Perfomance-Functions to query the
Processes...
See following dump:

0:000> ~*e k
ChildEBP RetAddr
WARNING: Frame IP not in any known module. Following frames may be wrong.
0012f6a8 791da717 0x6d4032b
0012f6b0 791da434 mscorwks!CallDescrWorker+0x30
0012f7c0 791da58a mscorwks!MethodDesc::CallDescr+0x1b6
0012f870 791da5f6 mscorwks!MethodDesc::CallDescr+0x43
0012f898 7923d587 mscorwks!MethodDesc::Call+0x97
0012f950 7923d342 mscorwks!ClassLoader::CanAccess+0x1d6
0012fa64 7923d441 mscorwks!ClassLoader::ExecuteMainMethod+0x49d
0012fa7c 7923d92f mscorwks!Assembly::ExecuteMainMethod+0x21
0012fd60 791c6e73 mscorwks!SystemDomain::ExecuteMainMethod+0x416
0012ffa0 791c6ef3 mscorwks!ExecuteEXE+0x1c0
0012ffb0 7917d0b8 mscorwks!_CorExeMain+0x59
0012ffc0 77e614c7 mscoree!_CorExeMain+0x30
0012fff0 00000000 KERNEL32!BaseProcessStart+0x23

ChildEBP RetAddr
06befee8 77f4c534 SharedUserData!SystemCallStub+0x4
06befeec 77e5a62d ntdll!NtWaitForSingleObject+0xc
06beff50 77e5ac21 KERNEL32!WaitForSingleObjectEx+0xa8
06beff60 791bbe7a KERNEL32!WaitForSingleObject+0xf
06beff70 791bbea4 mscorwks!WaitForFinalizerEvent+0xde
06beffb4 77e5d33b mscorwks!GCHeap::FinalizerThreadStart+0x96
06beffec 00000000 KERNEL32!BaseThreadStart+0x37

ChildEBP RetAddr
04a4fe7c 77f4c524 SharedUserData!SystemCallStub+0x4
04a4fe80 77e55ee0 ntdll!NtWaitForMultipleObjects+0xc
04a4ff1c 77e55faa KERNEL32!WaitForMultipleObjectsEx+0x12c
04a4ff34 791d25d5 KERNEL32!WaitForMultipleObjects+0x17
04a4ff9c 791d254c mscorwks!DebuggerRCThread::MainLoop+0x90
04a4ffac 791d4d50 mscorwks!DebuggerRCThread::ThreadProc+0x68
04a4ffb4 77e5d33b mscorwks!DebuggerRCThread::ThreadProcStatic+0xb
04a4ffec 00000000 KERNEL32!BaseThreadStart+0x37

ChildEBP RetAddr
06e7ff7c 77f4c534 SharedUserData!SystemCallStub+0x4
06e7ff80 77dc5e53 ntdll!NtWaitForSingleObject+0xc
06e7ffb4 77e5d33b ADVAPI32!PerflibTimerFunction+0x8b
06e7ffec 00000000 KERNEL32!BaseThreadStart+0x37

ChildEBP RetAddr
070bfea8 77f4c524 SharedUserData!SystemCallStub+0x4
070bfeac 77e55ee0 ntdll!NtWaitForMultipleObjects+0xc
070bff48 77e55faa KERNEL32!WaitForMultipleObjectsEx+0x12c
070bff60 7976cece KERNEL32!WaitForMultipleObjects+0x17
070bffb4 77e5d33b CorperfmonExt!HandlerAuxThreadProc+0x38
070bffec 00000000 KERNEL32!BaseThreadStart+0x37

ChildEBP RetAddr
0735fee0 77f4c534 SharedUserData!SystemCallStub+0x4
0735fee4 77e5a62d ntdll!NtWaitForSingleObject+0xc
0735ff48 77e5ac21 KERNEL32!WaitForSingleObjectEx+0xa8
0735ff58 79e78e40 KERNEL32!WaitForSingleObject+0xf
0735ffac 79e78126 aspnet_isapi!CPerfCounterClient::GatherPerfData+0x207
0735ffb4 77e5d33b aspnet_isapi!PerfDataGatherThreadStart+0xa
0735ffec 00000000 KERNEL32!BaseThreadStart+0x37

ChildEBP RetAddr
0725ff14 77f4c534 SharedUserData!SystemCallStub+0x4
0725ff18 77e5a62d ntdll!NtWaitForSingleObject+0xc
0725ff7c 77e5ac21 KERNEL32!WaitForSingleObjectEx+0xa8
0725ff8c 79e789ff KERNEL32!WaitForSingleObject+0xf
0725ffac 79e78113 aspnet_isapi!CPerfCounterClient::MonitorPerfPipeNames+
0x6b
0725ffb4 77e5d33b aspnet_isapi!RegistryMonitorThreadStart+0xa
0725ffec 00000000 KERNEL32!BaseThreadStart+0x37

ChildEBP RetAddr
0792fea4 77f4c524 SharedUserData!SystemCallStub+0x4
0792fea8 77e55ee0 ntdll!NtWaitForMultipleObjects+0xc
0792ff44 77da87a4 KERNEL32!WaitForMultipleObjectsEx+0x12c
0792ffb4 77e5d33b ADVAPI32!WmipEventPump+0x22e
0792ffec 00000000 KERNEL32!BaseThreadStart+0x37

ChildEBP RetAddr
07a2ff3c 77f4c534 SharedUserData!SystemCallStub+0x4
07a2ff40 77e5a62d ntdll!NtWaitForSingleObject+0xc
07a2ffa4 7350694c KERNEL32!WaitForSingleObjectEx+0xa8
07a2ffb4 77e5d33b TRAFFIC!GpcNotifyThreadFunction+0x15
07a2ffec 00000000 KERNEL32!BaseThreadStart+0x37

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server ?
http://sourceforge.net/projects/srvreport/
 
Back
Top