6

Possible Duplicate:
What are the other threads in a default VCL application, and can they be named by purpose?

When running a new empty VCL Forms Application in Delphi XE2 (32bit), I see 4 threads running in the Task Manager for this app. Obviously any app requires at least 1 thread, but in this case, what are the other 3 threads? I'd like to have a better understanding of what threads any VCL forms application runs by default. I thought possibly it had to be the fact that I was running in debug mode from RAD Studio, so I launched the EXE its self, and it also had 4 threads running. I also tried compiling under "release" config (thus disabling compiling debug info) and there are still 4 threads.

enter image description here

Community
  • 1
  • 1
Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327

2 Answers2

8

To determine the source of the threads, you can inspect the start address of the threads using a tool like process explorer or process hacker.

enter image description here

In this case for example you can see

  • ntdll.dll!TpCallbackIndependent+0x????? which is part of the Windows threadpool API.
  • ntdll.dll!RtlMoveMemory+0x????? is a call to the RtlMoveMemory WinAPi function.
  • Project??.Exe+0x????? Main thread of the App.
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • You also should See that in the IDE when you Pause the Debugger and then open the view "Thread Status". There you see all Threads which are currently Running. When you make a doubleclick in a thread then you See the callstack – Lars Jul 02 '12 at 04:59
  • @Lars unfortunally the Delphi IDE it seems doesn't resolve the names of the calling functions in the threads stack, only shows the memory address. – RRUZ Jul 02 '12 at 05:04
  • yes, you are right, I had no Delphi running when I wrote this comment. But anyway +1 for your anwser – Lars Jul 02 '12 at 06:32
  • 2
    Knowing where the threads start isn't really all that useful. Knowing that they are created by a call to `WTSRegisterSessionNotification` is. – David Heffernan Jul 02 '12 at 11:21
  • It would be a nice-citizen thing of Embarcadero's delphi team to name these stupid threads for me. Having unnamed threads out of the box on EVERY new Win7/64 delphi process is sloppy, in my opinion. Either that or else have their debugger get smarter, so it knows that the start address in ntdll.dll TpCallbackIndependant is equal to the WTSRegisterSesssionNotification threads, and name them in the IDE, appropriately. – Warren P Jul 04 '12 at 19:26
5

On my computer all the other threads than the main thread are created because the application's window registered to receive session change notifications with Wtsapi32.WTSRegisterSessionNotification API. You can see the implementation inside Vcl.Forms -> TApplication.CreateHandle procedure. It must have to do with how the application work/looks when you logged through Terminal Services/Remote Desktop. Some other threads could possible exist because some other program/s loaded code into your executable.

pani
  • 1,075
  • 6
  • 13
  • Why does TApplication register this session change notifications would be interesting to know – mjn Jul 02 '12 at 07:37
  • 1
    It is used to enumerate again the monitors available in Screen object. – pani Jul 02 '12 at 07:46