【问题标题】:Get count of open Thread of process in c#在c#中获取进程的打开线程数
【发布时间】: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


【解决方案1】:
p.Refresh();
var threadCount = p.Threads.Count;

【讨论】:

  • @user1983460 很高兴我能帮上忙
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-16
  • 2013-11-30
  • 2010-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多