0 windows service multithreading
我有一个多线程的Windows服务应用程序,我想知道我的应用程序创建了多少线程(具有线程ID,线程名称,相应的进程ID)正在运行.
预先感谢.
用C#
using System;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
Process proc = Process.GetCurrentProcess();
Console.WriteLine(proc.Threads.Count.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
如果你想要一些其他进程(不是当前应用程序)的工作线程,请使用GetProcessById(想要的proc)或GetProcessByName(想要的proc)更改GetCurrentProcess()
如果你想从线程中获得特定的东西
proc.Threads[index].[take a look at the properties];
Run Code Online (Sandbox Code Playgroud)
编辑:我怎样才能获得进程的所有线程名称?
using System;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
Process[] proc = Process.GetProcessesByName("youprocess");
Console.WriteLine(proc[0].Threads.Count.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
GetProcessesByName返回一个进程数组(可能有几个具有相同名称的进程).如果你确定只有一个 - proc [0]就是你想要的.如果有几个你可以使用索引访问它们 - proc [0],proc 1等,如果没有带有"你的名字"的进程,proc为null
| 归档时间: |
|
| 查看次数: |
6846 次 |
| 最近记录: |