有什么办法可以看到跨多个线程的完整堆栈跟踪吗? [英] Is there any way to see the full stack trace across multiple threads?

查看:72
本文介绍了有什么办法可以看到跨多个线程的完整堆栈跟踪吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#多线程编程中,当方法A()在新线程中调用方法B()时,例如通过使用类似这样的东西:

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.
}

换句话说,在方法B()中,堆栈跟踪对方法A()的调用路径一无所知,而方法A()的调用路径又触发了方法B()的执行.

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().

有什么方法可以查看完整逻辑堆栈跟踪,例如如果B()中有异常,您可以查看全文以了解A()实际上是否被称为它吗?

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?

推荐答案

通常,答案为No,因为根据定义,StackTrace不能包含来自其他堆栈的信息.但是,如果您在Visual Studio中调试应用程序,它将为您完成一些工作(这是C ++,但是在所有语言中都是相似的):

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):

在这里,外部代码比您的要暗,并且您可以查看一些父"代码.线程信息.但是,通常此屏幕没有太大帮助. Visual Studio创建vshost.exe文件以收集尽可能多的调试信息.

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.

此外,如果在创建任务时将它们附加到父项上,并且有一些异常,则将使用异常的ToString方法获得完整的堆栈跟踪,但仍然不是您想要的.

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.

这篇关于有什么办法可以看到跨多个线程的完整堆栈跟踪吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆