5

I'm working on a C# application and i'm facing some threads issues. I would like to know which threads are running in my application.

I use the code bellow :

foreach (ProcessThread s in Process.GetCurrentProcess().Threads)
{
  try
  {
    int l_iTest = 0;
    //(Thread) s.name
    //l_liTest += s.Id + ";"; <-- name of the ProcessThread ?
  }
  catch
  {
     // Some stuff
  }}

My point is, I would like to know which class or assambly has launched my thread.

Does anyone have an idea ?

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
Yann D.
  • 51
  • 1
  • 2
  • 1
    You can't go back from a ProcessThread to a Thread. Pass an argument if you need to know anything about the code that started the thread. – Hans Passant Oct 28 '11 at 13:44
  • @HansPassant: are you sure there isn't a way to go from the ProcessThread.Id to a Thread object? That would solve his problems and mines too. – Ignacio Soler Garcia May 30 '12 at 07:27

1 Answers1

0

Take a look at my answer to this question and see if a variant might be able to help you (i.e. your thread would have access to the global repository of known threads within the app).

Community
  • 1
  • 1
Jesse C. Slicer
  • 19,901
  • 3
  • 68
  • 87
  • As I understand, that code supposes to use the class to launch the thread whose name one wants to know? That definitely not a solution, e.g. I am using a background worker `RunWorkerAsync`, and I need to find that thread id by its name. – Hi-Angel Oct 15 '15 at 14:15
  • @Hi-Angel you should then open up a question with those details to get appropriate research and an answer. – Jesse C. Slicer Mar 19 '21 at 20:21
  • Thanks. It's been 6 years, I don't do C# these days, so I don't think I'm gonna need a solution. But in retrospective, the comment is apparently about a usecase that the question might include, however the answer does not cover. So, someone acknowledgeable might stumble upon this comment and write another answer here, which would cover such usecase. – Hi-Angel Mar 19 '21 at 22:00