How to get a parent thread from a child thread or another thread?

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

Is there a connection between threads in .NET? I urgently need to get the parent stream from the actual stream, and there is no way to pass this information. Is there a way to get the parent thread in .NET or using the Win32 API?

Is there a way to get the parent thread from any other thread?

+4
source share
5 answers

.NET does not record parent-child relationships between threads. You will need to track this data yourself, passing the parent stream to the child at creation.

However, I cannot imagine why you need this. Not Join from a parent or something good enough?

I can understand that you might be upset by questions such as "What are you trying to do, anyway?" - but often, if you tell us what your true goal is, we can find the best way that you did not think about. :)

+8
source

In Win32, threads do not have parent threads. All these processes belong to the process.

+4
source

Threads do not have parent / child relationships on the .NET platform. this is due to the fact that in Win32 all processes belong to a process.

You can impose this relationship on your own by creating a wrapper class around your threads. However, with such little information, I cannot recommend a solution.

+3
source

There is no connection between Threads in .NET. However, in the .NET 4 Task / Task<T> classes, there are parent-child relationships that are in many ways better suited for concurrency than directly using threads.

It is said ...

I urgently need to get the parent stream from the actual stream, and there is no way to pass this piece of information.

The main way to transfer information to a stream is to implement a SynchronizationContext . This is usually done using the user interface interfaces to implement ISynchronizeInvoke for objects inside this stream ( i.e .: Control.Invoke ). This is not something that is usually done manually, and requires a highly customizable thread that supports its own messaging pump or a process queue that is constantly being processed.

I would recommend considering changing this producer / consumer scenario instead and polling your child threads for new processing elements. This eliminates the need to know about β€œparents” - since any thread can add to the processing queue. The BlockingCollection<T> class is ideal for this scenario.

+1
source

Use InvokeRequired to perform some operation on the main thread.

-2
source

Source: https://habr.com/ru/post/1341650/


All Articles