我想要通过从我的应用程序运行的进程打开的线程计数运行应用程序我使用此代码
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;
}
}
有没有其他方法可以做到这一点?
答案 0 :(得分:2)
p.Refresh();
var threadCount = p.Threads.Count;