-1

fig refers to task manager Resource monitor

How to get whole list of processes in decreasing order of their thread count ( C# )

Community
  • 1
  • 1
METALHEAD
  • 2,734
  • 3
  • 22
  • 37

3 Answers3

4

You could try this,

Process[] processList = Process.GetProcesses().OrderByDescending(x => x.Threads.Count).ToArray();
Kurubaran
  • 8,696
  • 5
  • 43
  • 65
1

You could do the following:

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);
}
Gnqz
  • 3,292
  • 3
  • 25
  • 35
0

Its a big code (can become more compact)......but finally got the answer....thank you so much @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]);

            }                        

        }                          

    }    

}
METALHEAD
  • 2,734
  • 3
  • 22
  • 37