C#如何计算我的AppDomain中的托管线程? [英] C# How to count managed threads in my AppDomain?

查看:113
本文介绍了C#如何计算我的AppDomain中的托管线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以找出我使用了多少个托管线程(包括ThreadPool)?

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

当我通过GetProcess获得非托管线程的数量时,我会发现它的数量是疯狂的(一开始是21)

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

推荐答案

那不是它的工作方式.托管程序中的任何线程都可以执行托管代码,包括最初作为非托管线程启动的代码.主线程和任何线程池线程中的哪一个是最开始执行纯非托管代码的生命.通过Marshal.GetDelegateForFunctionPointer()提供的网关,它可以转换为托管代码.

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

看到数十个(否则处于非活动状态)线程并不罕见.它们通常是线程池线程和由COM服务器启动的线程. .NET缺少在这些线程上使用Thread.ManagedThreadId所需的管道.这是有意的,逻辑.NET线程不必是物理操作系统线程.尽管目前没有主机,但事实并非如此.

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.

这篇关于C#如何计算我的AppDomain中的托管线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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