如何按线程数(C#)的递减顺序获取整个进程列表
答案 0 :(得分:4)
你可以试试这个,
Process[] processList = Process.GetProcesses().OrderByDescending(x => x.Threads.Count).ToArray();
答案 1 :(得分:1)
您可以执行以下操作:
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);
}
答案 2 :(得分:0)
它是一个很大的代码(可以变得更紧凑)......但终于得到了答案....非常感谢@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]);
}
}
}
}