views:

309

answers:

2

I created a simple .NET windows application in Visual Studio 2005 and on just entering the main form load event my threads window is as in the following image:

http://img519.imageshack.us/my.php?image=threadshh4.jpg

My questions are

1)Why are there so many threads in the first place when I haven't started any(apart from my application's 'Main Thread')

2)What does this thread named '.Net SystemEvents' do?

3)Why are the entries under the column 'Location' for all threads except the Main Thread empty?

EDIT:
4) Is it possible to make these thread not start? or go away after some time?
5) What are they meant to do? what is their purpose?

+3  A: 

1) They're threads that are part of the managed framework.

2) It monitors for system events that you can register event handlers for, such as when you change your display settings and such.

3) Because they're part of the framework rather than your application code, so the source location isn't known by the debugger.

Gerald
A: 

Remember that there is a one to many relationship between unmanaged thread and managed threads.

There is a way to move SystemEvents notifier into your thread:

public static class ThreadingHelper_NativeMethods
{
   [DllImport("user32.dll")]
   public static extern bool IsGUIThread(bool bConvert);
}


     // This code forces initialization of .NET BroadcastEventWindow to the UI thread.
     // http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/fb267827-1765-4bd9-ae2f-0abbd5a2ae22
     if (ThreadingHelper_NativeMethods.IsGUIThread(false))
     {
        Microsoft.Win32.SystemEvents.InvokeOnEventsThread(new MethodInvoker(delegate()
        {
           int x = 0;
        }));
     }
GregC