none
Is there a way to see what threads are being created in .net C# RRS feed

  • Question

  • I have the following simple program in an effort to understand threads, and when I register methods to be called upon cancellation of the original activity I see one additional thread without a name or anything before the application quits. Can someone provide more insight to what is going on in the following code? I greatly appreciate it. 

    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace ScratchPad
    {
        class Program
        {
            static void Main(string[] args)
            {
                CancellationTokenSource cts = new CancellationTokenSource();
                cts.Token.Register(Method1);
                String resp = String.Empty;
                //Before you spin up a new thread ask if the user wants to proceed, and if they say no, cancel the operation 
                Console.WriteLine("Do you want to cancel the work? ");
                resp = Console.ReadLine();
                if (resp == "y") cts.Cancel();
                Object someState=cts.Token;
                ThreadPool.QueueUserWorkItem(o=>MyMethod(cts.Token));
                Console.WriteLine("Back to main method");
                Console.ReadLine();
            }
            private static void MyMethod(CancellationToken token)
            {
                //CancellationTokenSource myCancelationTokenSource = new CancellationTokenSource();
                //myCancelationTokenSource.Token = state;
                if (token.IsCancellationRequested)
                    Console.WriteLine("work was canceled");
                else 
                Console.WriteLine("In my Method");
            }
            private static void Method1()
            {
                Console.WriteLine("Method1, a registered method, was called because the operation was cancelled ");
            }
            public void Method2 ()
            {
    
            }
        }
    }

    I see this Thread when I put a break on line 20 of the code "if (resp == "y") cts.Cancel();" and debug the code.
    • Edited by Nu2Csharp Wednesday, June 26, 2019 5:59 PM added more explanation.
    Wednesday, June 26, 2019 5:55 PM

Answers

  • While running in the debugger you will often see unnamed threads as the debugger has its own stuff going on as well. For your threads you can use the handy, but seldom used, Thread.Name property to set a thread's name. This makes it easier to see which threads are yours. However this is generally only done when you're creating long running threads yourself. The ThreadPool threads are short lives so setting their name will cause you confusion later as the name isn't reset. However for debugging you could set it when the method is entered and clean it up on exit.

    Additionally stepping through your code line by line while the Threads and Parallel windows are open like Jack mentioned is useful for understanding threads. You can also look at the Thread.ManagedId to see when you are on different threads.

    In your particular example I would expect you to see your main thread and whatever threads the ThreadPool ends up creating. Since you only have 1 method being called outside your main thread everything else is from the runtime/debugger.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Nu2Csharp Wednesday, July 3, 2019 6:50 PM
    Wednesday, July 3, 2019 1:48 PM
    Moderator

All replies

  • You can use Process Explorer and like ant any given process' threads, like your programname.exe is a process.

    https://www.howtogeek.com/school/sysinternals-pro/lesson2/

    https://docs.microsoft.com/en-us/sysinternals/downloads/process-explorer


    • Edited by DA924x Wednesday, June 26, 2019 9:31 PM
    Wednesday, June 26, 2019 9:30 PM
  • Hi Nu2Csharp,

    Thank you for posting here.

    Based on your description, you want to monitor the threads created in c#.

    I suggest that you could use Parallel Stacks Window to view threads. You could refer to the following link to use Parallel Stacks Window.

    https://docs.microsoft.com/en-us/visualstudio/debugger/using-the-parallel-stacks-window?view=vs-2019

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 27, 2019 6:56 AM
    Moderator
  • While running in the debugger you will often see unnamed threads as the debugger has its own stuff going on as well. For your threads you can use the handy, but seldom used, Thread.Name property to set a thread's name. This makes it easier to see which threads are yours. However this is generally only done when you're creating long running threads yourself. The ThreadPool threads are short lives so setting their name will cause you confusion later as the name isn't reset. However for debugging you could set it when the method is entered and clean it up on exit.

    Additionally stepping through your code line by line while the Threads and Parallel windows are open like Jack mentioned is useful for understanding threads. You can also look at the Thread.ManagedId to see when you are on different threads.

    In your particular example I would expect you to see your main thread and whatever threads the ThreadPool ends up creating. Since you only have 1 method being called outside your main thread everything else is from the runtime/debugger.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Nu2Csharp Wednesday, July 3, 2019 6:50 PM
    Wednesday, July 3, 2019 1:48 PM
    Moderator