Paymon
Paymon

Reputation: 1193

Is there any way to see the full stack trace across multiple threads?

In C# multi-threaded programming, when method A() calls method B() in a new thread, e.g. by using something like this:

Task A()
{
    // ...

    // I want B to run in parallel, without A() waiting for it.
    Task.Factory.StartNew(B); 
}

void B()
{
     // I attach a debugger here to look at the Call Stack.
     // But that is empty and I can't see that A() actually called it (logically).

     // Also Environment.StackTrace is pretty much empty of that path.
}

In other words inside method B() the stack trace doesn't know anything about the call path to method A() which in turn triggered execute of method B().

Is there any way to see the full logical stack trace, so e.g. in the case of an exception in B() you can see the full story to know A() in effect called it?

Upvotes: 4

Views: 2388

Answers (1)

VMAtm
VMAtm

Reputation: 28355

In general the answer is No, as StackTrace by definition cannot contain information from other stack. However, if you debugging your application in Visual studio, it do some work for you (this is C++, but it's similar for all the languages):

enter image description here

Here, external code is dimmer than your, and you can review some "parent" thread information. However, usually this screen isn't much helpful. Visual studio creates vshost.exe file for gather as much debug information as possible.

Also, if you create the task withh attaching them to parent, and there is some exception, you'll get full stacktrace with exception's ToString method, but still it's not exactly what you want.

Upvotes: 1

Related Questions