none
C# : Find the thread ID of all the threads spawned by the main thread. RRS feed

  • Question

  • I have a scenario where the main thread spawns a thread (Thread A).

    This thread then spawns another thread (Thread B) and so on..

    Is there any way to identify all the threads spawned from the min thread using C#?

    Tuesday, June 4, 2013 12:49 PM

Answers

  • No, the code that runs on the spawned thread doesn't know anything about the main thread. You may consider to give the spawned thread some useful name before staring it in order to able to identify it though.
    Tuesday, June 4, 2013 1:00 PM

  • Is there any way to identify all the threads spawned from the min thread using C#?

    http://msdn.microsoft.com/en-us/library/system.threading.thread.managedthreadid.aspx

    Regards,

      Thorsten

    Tuesday, June 4, 2013 1:19 PM
  • like:

    [The managedthreadId is available when the thread is instanciated, so you could save these to later identify the threads]

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                Thread t = new Thread(new ThreadStart(DoTheJob));
                MessageBox.Show(t.ManagedThreadId.ToString());
                t.Start();   
                Thread t2 = new Thread(new ThreadStart(DoTheJob));
                t2.Start();
    
                MessageBox.Show(t.ManagedThreadId.ToString());  
                MessageBox.Show(t2.ManagedThreadId.ToString());
            }
    
            void DoTheJob()
            {
                for (int i = 0; i < 100; i++)
                    Thread.Sleep(100);
            }
        }

    Regards,

      Thorsten


    Tuesday, June 4, 2013 1:21 PM

All replies

  • No, the code that runs on the spawned thread doesn't know anything about the main thread. You may consider to give the spawned thread some useful name before staring it in order to able to identify it though.
    Tuesday, June 4, 2013 1:00 PM

  • Is there any way to identify all the threads spawned from the min thread using C#?

    http://msdn.microsoft.com/en-us/library/system.threading.thread.managedthreadid.aspx

    Regards,

      Thorsten

    Tuesday, June 4, 2013 1:19 PM
  • like:

    [The managedthreadId is available when the thread is instanciated, so you could save these to later identify the threads]

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                Thread t = new Thread(new ThreadStart(DoTheJob));
                MessageBox.Show(t.ManagedThreadId.ToString());
                t.Start();   
                Thread t2 = new Thread(new ThreadStart(DoTheJob));
                t2.Start();
    
                MessageBox.Show(t.ManagedThreadId.ToString());  
                MessageBox.Show(t2.ManagedThreadId.ToString());
            }
    
            void DoTheJob()
            {
                for (int i = 0; i < 100; i++)
                    Thread.Sleep(100);
            }
        }

    Regards,

      Thorsten


    Tuesday, June 4, 2013 1:21 PM
  • Naming your threads is the best way to easily identify them.  Even better is that the thread names show up in the Threads window in the debugger so when something goes wrong you can more easily get contextual info based upon the thread name.  For example in one app I wrote the thread name corresponded to a server connection while other threads used client connections.  By looking at the thread name we immediately knew whether the error was occurring server to server or server to client without ever having to look at the call stack.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Tuesday, June 4, 2013 1:30 PM
    Moderator