C# 如何计算 AppDomain 中的托管线程数?

发布于 2024-09-06 07:14:51 字数 91 浏览 6 评论 0原文

有没有办法找出我使用了多少托管线程(包括线程池)?

当我通过 GetProcess 获取非托管线程的数量时,我得到了一个疯狂的数量(一开始是 21 个)

Is there a way to find out how many managed thread I use (including ThreadPool)?

When I get count of unmanaged threads through GetProcess I have an insane number of it (21 at very beginning)

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

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

发布评论

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

评论(2

浅浅 2024-09-13 07:14:51

这不是它的工作方式。托管程序中的任何线程都可以执行托管代码,包括最初作为非托管线程启动的代码。其中大多数是主线程和任何线程池线程开始执行纯粹的非托管代码。它通过 Marshal.GetDelegateForFunctionPointer() 提供的网关类型插入托管代码。

看到数十个(否则不活动的)线程并不罕见。它们通常是线程池线程和由 COM 服务器启动的线程。 .NET 缺少在这些线程上使用 Thread.ManagedThreadId 所需的管道。这是故意的,逻辑 .NET 线程不必是物理操作系统线程。尽管当前使用的主机中没有不是这种情况的。

你必须避免问这个问题。

That's not the way it works. Any thread in a managed program can execute managed code, including ones that were originally started as an unmanaged thread. Which most are, the main thread and any threadpool thread starts life executing purely unmanaged code. It thunks into managed code though the kind of gateway provided by Marshal.GetDelegateForFunctionPointer().

Seeing dozens of (otherwise inactive) threads is not unusual. They typically are threadpool threads and threads started by COM servers. .NET is missing the plumbing you'd need to use Thread.ManagedThreadId on those threads. That's intentional, a logical .NET thread doesn't have to be a physical operating system thread. Although there's no host in current use where that's not the case.

You will have to avoid having to ask the question.

鹤仙姿 2024-09-13 07:14:51

我还没有检查是否可以使用调试接口,但由于 VS 在其调试器中显示托管线程,因此您也应该能够在您的调试器中获取它们。

在 .NET 中,编写调试器比您想象的要容易得多。实现调试器基本上包括实现ICorDebug 接口。

有一个来自 Microsoft 的示例: 托管调试器示例

I have not checked whether it is possible using the debugging interfaces but since VS displays managed threads in its debugger, you should be able to get them in yours too.

In .NET, writing a debugger is much easier than you may expect. Implementing the debugger basically consists of implementing the ICorDebug interface.

There is a sample from Microsoft: Managed Debugger Sample

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