8

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

Is there any relationship between threads in .NET? I urgently need to get the parent thread from the actual thread and there is no way to handover this piece of information. Is there any way to get parent thread in .NET or using the Win32 API?

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

Community
  • 1
  • 1
Martin Macak
  • 3,507
  • 2
  • 30
  • 54
  • 1
    How would you define "parent thread"? What kind of environment are you operating in? What is the problem you are trying to solve? – Fredrik Mörk Feb 28 '11 at 13:54
  • We need more information about what you are trying to achieve to be able to give you the answer you're looking for. – Jon Grant Feb 28 '11 at 13:54
  • 8
    And you should accept that people will ask "why do you need this?" Too many questions on here are of the form "how do I get to Mars without using a spacecraft?" We're going to want some context to work out what you really need/want. – Roger Lipscombe Feb 28 '11 at 13:55
  • As you are not entirely sure its possible I'm asking it anyway. What are you trying to do? Enlighten us – Ralf de Kleine Feb 28 '11 at 13:55
  • 2
    Protip: In programming, if there isn't an easy path to do something*, its usually a hint that what you're trying to do is wrong. (*For the benefit of nitpickers: This does not, of course, include things that are hard to do on their face, such as converting an html document to pdf) –  Feb 28 '11 at 14:00

5 Answers5

10

.NET does not record parent-child relationships between threads. You will need to track this data on your own, by passing the parent thread to the child on creation.

That said, I cannot possibly imagine why you would need this. Isn't Joining from the parent or something good enough?

I can understand that you might be frustrated by questions like "What are you trying to do, anyway?" - but often times if you let us know what your true goal is we may be able to find a better way that you didn't think of. :)

bdonlan
  • 224,562
  • 31
  • 268
  • 324
4

In Win32, threads don't have parent threads. Threads are all owned by the process.

Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
3

Threads do not inherently have a parent/child relationship on the .NET framework. this is due to the fact that, in Win32, all threads are owned by the process.

You can impose this relationship yourself, by creating a wrapper class around your threads. However, with such little information, I can't recommend a solution.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
John Gietzen
  • 48,783
  • 32
  • 145
  • 190
1

There is no relationship between Threads in .NET. However, there is a parent-child relationship in .NET 4's Task/Task<T> classes, which are, in many ways, a better approach to concurrency than directly using threads.

That being said...

I urgently need to get the parent thread from the actual thread and there is no way to hand-over this piece of information.

The main way to "hand off" information to a thread would be to implement a SynchronizationContext. This is typically done by user interface frameworks in order to implement ISynchronizeInvoke on objects within that thread (ie: Control.Invoke). This is not something typically done manually, and requires a very customized thread that maintains it's own messaging pump or process queue which is continually being processed.

I would recommend considering changing this to a producer/consumer scenario instead, and having your "child" thread poll for new processing items. This eliminates all need to know about "parents" - since any thread can add to the processing queue. The BlockingCollection<T> class is ideal for this scenario.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Thanks, I am really aware of that all but my problem is bound to threads as I am digging into legacy infrastructure of our system. I don't need to synchronize threads, i need to determine the assembly that is in direct/indirect invocation chain to the current thread and that is compromised if thread that originates the task created a worker thread. Simply, there is no help for me.Thanks for your answer. – Martin Macak Feb 28 '11 at 14:47
-3

Use InvokeRequired to do some operation in main thread.

hungryMind
  • 6,931
  • 4
  • 29
  • 45