如何获取在多线程 Windows 服务应用程序中运行的线程数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1345797/
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
How to get how many threads are running in multithreaded windows service application
提问by Oscar Castiblanco
I have a multithreaded windows service application, I want to know every moment how many threads(with thread id, thread name, corresponding process id) are running which are created by my application.
我有一个多线程的 Windows 服务应用程序,我想知道每时每刻有多少线程(带有线程 id、线程名称、相应的进程 id)正在运行,这些线程是由我的应用程序创建的。
Thank in advance.
预先感谢。
回答by Svetlozar Angelov
With C#
使用 C#
using System;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
Process proc = Process.GetCurrentProcess();
Console.WriteLine(proc.Threads.Count.ToString());
}
}
If you want working threads for some other process(not current app) change GetCurrentProcess() with GetProcessById(wanted proc) or GetProcessByName(wanted proc)
如果您希望其他进程(不是当前应用程序)的工作线程使用 GetProcessById(wanted proc) 或 GetProcessByName(wanted proc) 更改 GetCurrentProcess()
If you want to get something specific from the threads
如果你想从线程中得到一些特定的东西
proc.Threads[index].[take a look at the properties];
EDIT: How can I get all threads name of the process?
编辑:如何获取进程的所有线程名称?
using System;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
Process[] proc = Process.GetProcessesByName("youprocess");
Console.WriteLine(proc[0].Threads.Count.ToString());
}
}
GetProcessesByNamereturns an array of processes(there may be several processes with the same names). If you are sure there is only one - proc[0] is what you want. If There are several you can access them with index - proc[0], proc1, etc.... proc is null if there are not processes with "your name"
GetProcessesByName返回一个进程数组(可能有多个同名的进程)。如果您确定只有一个 - proc[0] 就是您想要的。如果有几个您可以使用索引访问它们 - proc[0], proc 1等......如果没有带有“您的名字”的进程,则 proc 为空
回答by Svetlozar Angelov
A great utility for doing what you describe is Process Explorer:
执行您所描述的操作的一个很好的实用程序是 Process Explorer:
http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx
http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx
回答by Oscar Castiblanco
By using the windows task manager
通过使用 Windows 任务管理器
Ctrl-Alt-Del
View->SetColumns
Check Thread Count