How to get whole list of processes in decreasing order of their thread count ( C# )
Asked
Active
Viewed 279 times
-1
-
post your efforts in finding answer . codes – Tharif Jun 01 '15 at 06:03
-
1'Based on' isn't really a query condition. – H H Jun 01 '15 at 06:05
-
I 've used this.........as code but not working... – METALHEAD Jun 01 '15 at 06:09
-
` Process[] processlist = Process.GetProcesses(); foreach(Process theprocess in processlist){ Console.WriteLine(“Process: {0} ID: {1}”, theprocess.ProcessName, theprocess.Id); }` – METALHEAD Jun 01 '15 at 06:09
-
1What about https://msdn.microsoft.com/en-us/library/system.diagnostics.process.threads%28v=vs.110%29.aspx – DerApe Jun 01 '15 at 06:10
-
1edit question with codes in comments – Tharif Jun 01 '15 at 06:16
-
I mean How to get list of processes in decreasing order of thread count in C#......is this possible in C#? – METALHEAD Jun 01 '15 at 06:21
3 Answers
4
You could try this,
Process[] processList = Process.GetProcesses().OrderByDescending(x => x.Threads.Count).ToArray();

Kurubaran
- 8,696
- 5
- 43
- 65
-
@Kurubaran.....I am getting only one process with this...rundll32.exe which has only 1 thread.... – METALHEAD Jun 01 '15 at 08:55
-
@METALHEAD You meant above code retruns only one process ? if then something should be wrong. I tested that code and it works fine. – Kurubaran Jun 01 '15 at 09:24
-
-
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