来自不同线程上的异常的 StackTrace?

发布于 2024-08-04 21:45:23 字数 266 浏览 3 评论 0原文

StackTrace 上有一个构造函数,它将异常作为参数。一切都很好,但我注意到所有其他构造函数都说它将从当前线程获取 StackTrace,但是接受异常的构造函数除了

生成的堆栈跟踪描述了 当时的堆栈 异常。

我只是想确保,即使当我从另一个线程中的异常创建 StackTrace 时,我在创建异常的不同线程上创建 StackTrace,我也会获得正确的异常 StackTrace。

有人可以确认一下吗?

There is a constructor on StackTrace that takes an exeption as an argument. All fine and well, but I noticed that all the other constructors say that it will get the StackTrace from the current thread, but the constructor taking the exception does not say anything about that other than

The resulting stack trace describes
the stack at the time of the
exception.

I just want to be sure that I will get the correct StackTrace of an exception even if I create the StackTrace on a different thread that the exception was created in when I create the StackTrace from an exception in another thread.

Can someone please confirm?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

油焖大侠 2024-08-11 21:45:23

看起来很容易尝试,除非我错过了一些东西。这会为我打印正确的堆栈跟踪。

static Exception threadEx;

static void Main()
{
    Thread worker = new Thread(DoWork);
    worker.Start();
    worker.Join();

    if (threadEx != null) {
        StackTrace trace = new StackTrace(threadEx);
        Console.WriteLine(trace);
    }
}

static void DoWork()
{
    try {
        throw new Exception("Boom!");
    }
    catch (Exception ex) {
        threadEx = ex;
    }
}

Seems easy enough to try, unless I'm missing something. This prints the right stack trace for me.

static Exception threadEx;

static void Main()
{
    Thread worker = new Thread(DoWork);
    worker.Start();
    worker.Join();

    if (threadEx != null) {
        StackTrace trace = new StackTrace(threadEx);
        Console.WriteLine(trace);
    }
}

static void DoWork()
{
    try {
        throw new Exception("Boom!");
    }
    catch (Exception ex) {
        threadEx = ex;
    }
}
逆光飞翔i 2024-08-11 21:45:23

它将为其调用的线程创建 StackTrace(在内部,它使用参数“targetThread”调用“CaptureStackTrace”,该参数指示当前线程被请求)。为另一个线程创建的唯一构造函数是采用 Thread 实例的构造函数。

It will create the StackTrace for the thread it's called on (internally, it calls 'CaptureStackTrace' with for the parameter 'targetThread', which indicates the current thread is requested). The only ctor which creates for another thread is the one which takes a Thread instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文