【问题标题】:How to get whole list of processes in decreasing order of their thread count ( C# ) [duplicate]如何按线程数的递减顺序获取整个进程列表(C#)[重复]
【发布时间】:2015-06-02 13:39:34
【问题描述】:

如何按线程数的递减顺序获取整个进程列表 (C#)

【问题讨论】:

  • 发布您为寻找答案所做的努力。代码
  • “基于”并不是真正的查询条件。
  • 我已经用这个............作为代码但不起作用......
  • 用 cmets 中的代码编辑问题

标签: c# .net multithreading process


【解决方案1】:

你可以试试这个,

Process[] processList = Process.GetProcesses().OrderByDescending(x => x.Threads.Count).ToArray();

【讨论】:

  • @Kurubaran .....我得到的只有一个进程......rundll32.exe 只有一个线程......
  • @METALHEAD 你的意思是上面的代码只重新运行一个进程?如果那么应该是错误的。我测试了该代码,它工作正常。
  • 谢谢您的回答
  • @METALHEAD 不客气。
【解决方案2】:

您可以执行以下操作:

private void fuc()
{
    System.Diagnostics.Process[] procArray;
    procArray = System.Diagnostics.Process.GetProcesses();
    List<KeyValuePair<string, int>> threads = new List<KeyValuePair<string,int>>();
    for (int i = 0; i < procArray.Length; i++)
    {
        var element = new KeyValuePair<string, int>(procArray[i].ProcessName, procArray[i].Threads.Count);
        threads.Add(element);
    }
    threads.Sort(OrderAsc);
}

static int OrderAsc(KeyValuePair<string, int> a, KeyValuePair<string, int> b)
{
     return a.Value.CompareTo(b.Value);
}

【讨论】:

    【解决方案3】:

    它的代码很大(可以变得更紧凑)......但终于得到了答案......非常感谢@kuruban @Gnqz @utility @derape

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Diagnostics;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                System.Diagnostics.Process[] procArray;
                procArray = System.Diagnostics.Process.GetProcesses();
                String[,] arr = new String[300,2];
                String max, maxi;
                int k;
                for (k = 0; k < procArray.Length; k++)
                {
                    arr[k, 0] = procArray[k].ProcessName;
                    arr[k, 1] = (procArray[k].Threads.Count).ToString();
                }
    
                for (int i = 0; i < procArray.Length; i++)
                 {
                     for (int j = i; j < procArray.Length; j++)
                      {
                         if (int.Parse(arr[i, 1]) < int.Parse(arr[j, 1]))
                          {
                            max = arr[j, 0];
                            arr[j, 0] = arr[i, 0];
                            arr[i, 0] = max;
                            maxi = arr[j, 1];
                            arr[j, 1] = arr[i, 1];
                            arr[i, 1] = maxi;
                          }
                      }
    
                }
                for (int i = 0; i < procArray.Length; i++)
                {
                   Console.WriteLine("{0} {1}", arr[i, 0], arr[i, 1]);
    
                }                        
    
            }                          
    
        }    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-24
      • 2013-09-10
      • 1970-01-01
      相关资源
      最近更新 更多