如何枚举 C# 中的所有托管线程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/466799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 04:32:44  来源:igfitidea点击:

How can I enumerate all managed threads in C#?

c#multithreading

提问by Jon Tackabury

Is it possible to enumerate all managed threads in C#? Visual Studio seems to be able to do this when you hit a break point while debugging. In the "Threads" window it shows a list of all running threads, including managed names. Does anyone know how it does this?

是否可以枚举 C# 中的所有托管线程?当您在调试时遇到断点时,Visual Studio 似乎能够做到这一点。在“线程”窗口中,它显示了所有正在运行的线程的列表,包括托管名称。有谁知道它是如何做到的?

采纳答案by Jon Skeet

Debuggers are often able to do things "normal" code can't. You'd probably find that you can do this if you use the profiling/debugging API, but I don't believe you can do it from "normal" .NET code.

调试器通常能够做“普通”代码做不到的事情。如果您使用分析/调试 API,您可能会发现您可以做到这一点,但我不相信您可以通过“普通”.NET 代码做到这一点。

回答by Marc Gravell

This sounds like a duplicate of "How to enumerate threads in .NET using the Name property?" - If so, the short answer is "keep track of your own threads yourself" - i.e. in a List<Thread>or similar.

这听起来像是“如何使用 Name 属性枚举 .NET 中的线程?” - 如果是这样,简短的回答是“自己跟踪您自己的线程” - 即在 aList<Thread>或类似内容中。

回答by Mauricio Scheffer

Take a look at Managed Stack Explorer:

看看托管堆栈资源管理器

MSE works by quickly attaching to a process when a stack trace is requested and the detaching the second the stack trace has been retrieved. This way the interference in the normal operation of the process is minimized.

MSE 的工作方式是在请求堆栈跟踪时快速附加到进程,并在检索到堆栈跟踪时分离。这样对过程正常运行的干扰被最小化。

Unfortunately, this means it hasto done by an external process.

不幸的是,这意味着它必须由外部进程完成。

A similar tool is StackDump, which uses MDbgto generate the dump.

一个类似的工具是StackDump,它使用MDbg生成转储。