Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Is there a way to get the parent thread id?

Suppose the main thread is spawning a new thread t1, how can my code that runs on t1 find the thread id of the main thread (using c#)?

Edit:
I don't control the creation of the new thread. So I can't pass any parameters to the thread.

Thanks.

like image 854
Ohad Horesh Avatar asked Nov 18 '10 11:11

Ohad Horesh


People also ask

How do you find parent threads?

The java. lang. ThreadGroup. getParent() method returns the parent of this thread group.

What is the parent thread?

Thread CreationThe creating thread is the parent thread, and the created thread is a child thread. Note that any thread, including the main program which is run as a thread when it starts, can create child threads at any time.

Do threads have parent/child relationship?

No. There is no copy of parent stack which is passed on to child.

What is thread in .NET core?

Thread is a foreground thread. You have tasks that cause the thread to block for long periods of time. The thread pool has a maximum number of threads, so a large number of blocked thread pool threads might prevent tasks from starting. You need to place threads into a single-threaded apartment.


1 Answers

You can't.

Yet you might consider:

  1. Prefix the name of the new thread with the thread ID from the parent thread
  2. Create a constructor on the method you want to spawn that requires the thread ID from the parent
like image 53
Jan Jongboom Avatar answered Oct 27 '22 01:10

Jan Jongboom