【发布时间】:2014-04-07 18:37:01
【问题描述】:
我想获取由我从我的应用程序运行的进程打开的线程数,以运行我使用此代码的应用程序
p.StartInfo = new ProcessStartInfo(Application.StartupPath + @"\bin\childApp.exe", parametr);
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
我想知道我运行这个应用程序打开了多少线程 我使用这个代码来工作,但是每秒使用它时 CPU 使用率非常高
private int GetThread(string AppId)
{
try
{
string queryString = "select ThreadCount from Win32_Process WHERE ProcessId='" + AppId + "'";
SelectQuery query = new SelectQuery(queryString);
ManagementScope scope = new System.Management.ManagementScope(@"\\.\root\CIMV2");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection processes = searcher.Get();
int result = 0;
foreach (ManagementObject mo in processes)
{
result = Convert.ToInt32(mo["ThreadCount"]);
break;
}
return result;
}
catch
{
return 0;
}
}
还有其他方法吗?
【问题讨论】:
-
不要每秒都调用它。每 15 秒调用一次 GetThread 计数。您的 CPU 使用率仍然很高吗?
标签: c# windows multithreading