Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 579753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:23:58+00:00 2026-05-13T14:23:58+00:00

Sometimes I find myself stepping through an application in Debug mode, until I hit

  • 0

Sometimes I find myself stepping through an application in Debug mode, until I hit ‘step’ on some particular line and it takes way too much time doing something, eating up 100% CPU. At this point, I hit the ‘Break’ button and try to find what’s running that’s taking so long.

The problem is, this app has a hefty amount of threads running, and at the time I hit ‘Break’, the execution point goes to the GUI thread that’s probably just doing a ‘Wait’.
I then have to hunt through the existing threads (I counted them – this time they’re 37!) trying to find the one I was executing on. I’d have to look at the stack of every one of them until I find the one I was looking for.

The thread I’m running on is an async invoke, so it runs on a thread pool thread.
I’d like to give this thread a descriptive name and reset its name at the end of the operation.

The problem is, property Thread.Name can only be set once, afterwards it gives an InvalidOperationException.

Any suggestions?

Oh yeah, I’m running VS2005/.NET 2.0 but I’m also curious if newer versions have better ways to handle this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:23:59+00:00Added an answer on May 13, 2026 at 2:23 pm

    Define your own threadstatic property

    public class ThreadMarker:IDisposable
    {
      [ThreadStatic]
      private static string __Name;
    
      static Dictionary<int,string> ThreadNames=new Dictionary<int,string>();
    
      public static Name{get{return __Name;}}
    
      public ThreadMarker(string name)
      {
        lock(ThreadNames){
          ThreadNames[Thread.CurrentThread.ManagedThreadId]=name;
        }
        __Name=name; 
      }
    
      public void Dispose()
      {
        ThreadNames.Remove(Thread.CurrentThread.ManagedThreadId);
        __Name="Unowned";
      } 
    }
    

    You can even write your own wrapper that automagically wraps your action / delegate / async callback in this using statement.

    class NamedHandler<TArg>{
      public readonly Action<TArg> Handler;
    
      NamedHandler(string name,Action<TArg> handler){
    
        Handler=arg=>{
          using(new ThreadMarker(name)){
            handler(arg);
          }
        }     
      }
    }
    
    // usage
    void doStuff(string arg){
      Log("Stuf done in thread {0} ",ThreadMarker.Name);
    }    
    
    ThreadPool.QueueUserWorkItem(new NamedHandler<string>("my Thread",arg=>DoStuff(arg)).Handler); 
    

    Then when you stop the debugger, have a look at the contents of the variable ThreadMarker.ThreadNames and you will see which managed threads are stuck in your named handlers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Once again I find myself failing at some really simple task in C++. Sometimes
This could save me so much time. Sometimes I find myself writing things like
Sometimes I find that preprocessor directives aren't flexible enough for my application, so I've
Sometimes I need to find some strings inside DB usually it is just host-name
I sometimes find myself writing code like this: someFunc :: Foo -> Int someFunc
When writing comments, I sometimes find myself needing to talk about a type (class,
Sometimes, I find myself in a situation where valid files are made invalid by
I find myself converting between array and object all the time in PHP application
I often find myself using lambdas as some sort of local functions to make
Sometimes I find myself needing to do something that could be automated or could

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.