从 ProcessThread 到托管线程

发布于 2024-08-12 02:36:19 字数 407 浏览 3 评论 0原文

我们经常会在生产环境中遇到 Windows 服务关闭的问题,但我们无法重现。可能要过几个月才会再次发生这种情况。

我正在进行一些诊断来尝试帮助解决该问题,我正在考虑的一件事是在我们启动应用程序关闭后 60 秒内向系统线程池添加一个事件。我们的应用程序应该最多在 10 秒内完全关闭。

在这种情况下,我想在事件日志中跟踪进程剩余的正在运行的线程。

我可以使用 System.Diagnostics.Process.GetCurrentProcess.Threads 获取正在运行的线程。这些线程对象具有本机 Win32 线程 ID 等。

我想知道是否有任何方法可以从这些线程 ID 返回到它们在当前进程中表示的任何托管线程。 我尝试这样做的原因是因为对于我们的线程池和我们生成的其他线程,我们给出了代表其用途的名称,这确实有助于恢复它们。

Periodically we get a hang on shut down of a Windows service in a production environment that we just cannot reproduce. It can be months before it happens again.

I'm putting in some diagnostics to try and help with the issue, one thing I'm looking at is adding an event to the system thread pool for 60 seconds after we initiate shut down of the application. Our application should shutdown cleanly within 10 seconds maximium.

In this event I would like to trace out the remaining running threads of the process to the event log.

I can get the running threads using System.Diagnostics.Process.GetCurrentProcess.Threads. These threads objects have native Win32 thread id's etc.

I was wondering if there was any way of getting back from these thread id's to any managed threads that they represent in the current process.
The reason I am trying to do this is because for our thread pools and other threads we spawn we give the names which represent their purpose and it would really help to get these back.

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

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

发布评论

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

评论(2

筑梦 2024-08-19 02:36:19

由于以下原因这是不可能的。 引自 MSDN

操作系统 ThreadId 没有
与托管的固定关系
线程,因为非托管主机可以
控制之间的关系
托管和非托管线程。
具体来说,复杂的主机可以
使用 CLR Hosting API 进行调度
许多托管线程针对同一线程
操作系统线程,或者移动一个
不同之间的托管线程
操作系统线程。

因此,托管线程和操作系统线程之间不存在一对一的映射。

那么现在的问题就变成了,如何获取当前进程中当前运行的所有托管线程的列表?不幸的是我不知道答案。

This is impossible for the following reason. Quote from MSDN:

An operating-system ThreadId has no
fixed relationship to a managed
thread, because an unmanaged host can
control the relationship between
managed and unmanaged threads.
Specifically, a sophisticated host can
use the CLR Hosting API to schedule
many managed threads against the same
operating system thread, or to move a
managed thread between different
operating system threads.

So there's no one-to-one mapping between managed threads and OS threads.

So now the question becomes, how to get a list of all the managed threads currently running in the current process? Unfortunately I don't know the answer.

黒涩兲箜 2024-08-19 02:36:19

看起来这是不可能的。
幸运的是,我们有自己的线程库,它包装了 .NET 库,并且我能够添加功能来保留可用于这些诊断的活动线程列表。
这工作得很好,但当然需要付出相当多的努力和测试,但希望我们能弄清楚它的真相。

It does not look like this is possible.
Luckily for us we have our own threading libraries which wrap the .NET libraries and I was able to put in functionality to keep a list of active threads that we can use for these diagnostics.
This works quite well but there was a fair bit of effort involved and testing of course, but hopefully we'll get o the bottom of it.

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