C# - Get processes sorted by processor load usage - c#

What's the lighest way to get a running processes list sorted by processor load? (kind of same as task manager but only sorted by processor usage load)
Lighest is the key word, because i want to make an updated and refreshed list every 2 seconds or something like that.
EDIT : using windows 7

I would recommend reading into the Process class
- System.Diagnostics.Process
Documentation
I would recommend looking at the GetProcesses() method within here.

Related

VB.Net (or C#), how to have about 100 threads do 10.000 jobs?

I have about 10.000 jobs that I want to be handled by approx 100 threads. Once a thread finished, the free 'slot' should get a new job untill there are no more jobs available.
Side note: processor load is not an issue, these jobs are mostly waiting for results or (socket) timeouts. And the amount of 100 is something that I am going to play with to find an optimum. Each job will take between 2 seconds and 5 minutes. So I want to assign new jobs to free threads and not pre-assign all jobs to threads.
My problem is that I am not sure how to do this. Im primarily using Visual Basic .Net (but C# is also ok).
I tried to make an array of threads but since each job/thread also returns a value (it also takes 2 input vars), I used 'withevents' and found out that you cannot do that on an array... maybe a collection would work? But I also need a way to manage the threads and feed them new jobs... And all results should go back to the main-form (thread)...
I have it all running in one thread, but now I want to speed up.
And then I though: Actually this is a rather common problem. There is a bunch of work to be done that needs to be distributed over an amount of worker threads.... So thats why I am asking. Whats the most common solution here?
I tried to make it question as generic as possible, so lots of people with the same kind of problem can be helped with your reply. Thanks!
Edit:
What I want to do in more detail is the following. I currently have about 1200 connected sensors that I want to read from via sockets. First thing I want to know is if the device is online (can connect on ip:port) or not. After it connects it will be depending on the device type. The device type is known after connect and Some devices I just read back a sensor value. Other devices need calibration to be performed, taking up to 5 minutes with mostly wait times and some reading/setting of values. All via the socket. Some even have FTP that I need to download a file from, but that I do via socket to.
My problem: Lot's of waiting time, so lot's of possibility to do things paralel and speed it up hugely.
My starting point is a list of ip:port addresses and I want to end up with a file with that shows the results and the results are also shown on a textbox on the main form (next to a start/pause/stop button)
This was very helpfull:
Multi Threading with Return value : vb.net
It explains the concept of a BackgroundWorker which takes away a lot of the hassle. I am now trying to see where it will bring me.

WMI event processor useage vs WMI query

I want to be able to detect a change in the list of installed programs on a pc using wmi.
I have 2 options 1- run a wmi query every X seconds and compare to a saved file containing the list. 2 - start a Wmi event that polls every X seconds.
Which uses less processing power give that I would like X to be 60 seconds?
As it's a 'versus' :
create a 'Task Schedule' triggering on "event:NewApp"
WMI for peek any changes is so heavy processing.
"EventLogs + Scheduling a Task "are really relevant. ( if enabled )
The performance difference between the two will be fairly negligible.

Why does opening a form takes longer the first time?

I have made a simple search utility that will search for files in your computer.
It has a function search which searches for the files and creates the list of matched items to a mainloop function which in turn calls displayForm function which displays the results of the search in a new form.
Whenever, I run the application the first time after startup, although the search function completes the search in about 1 seconds, the time taken to display the result window takes considerable time(about 10 seconds) . This happens only for the first time you search after startup.
I am not providing any code for search function because I don't think search function matters because It takes almost same amount of time irrespective of running it the first time or subsequent times, and the working of displayForm is very simple.
public void displayForm()
{
// Do some stuff here
// Make a listbox and add items to display.
SearchForm.ShowDialog() ;
}
Also, by experimenting with a few cases , I must tell you that making a list box takes the same time irrespective of running it first time or subsequent times.
What could be the possible reasons for this ?
This is entirely normal, it has little to do with your code. Cold start time is dominated by the speed of the hard disk. Which can be disappointing when it has to locate the many DLLs that are required to start a .NET app. Not a problem exclusive to .NET apps, large unmanaged apps like Office apps and Adobe Reader have this issue as well. Which tend to cheat by installing an "optimizer", a program that runs at login and makes your machine slow by pre-loading the DLLs that the program needs so they are available in the file system cache, defeating SuperFetch in the process.
The operating system's file system cache is a pretty effective solution for the slow disk, but it is like a mile long freight train to get up to speed. Getting it filled from scratch with useful data takes a while, effective disk transfer rates when it has to seek is at best a few megabytes/sec. Also the core reason that users like an SSD so much, it provides a much more fundamental solution. Once you've experienced one, you can never go back.
Covered very well in many excellent articles, best way to find them is by googling ".NET cold start time".

Monitor all process spawn and exit events on windows

I have to implement an application/service for Windows 7 or 8 that will keep track of all the processes that were spawned during a timespan and also their exit times.
Can someone please let me know if Windows OS provides any callback mechanism which will be called whenever a new process has been created and exited.
Idea is to show trend of usage of different applications through out the day or timespan.
This application is not intended for any marketplace or commercial use but for my own use. So any kind of suggestion will be very useful.
Thanks
Even such professional tools like Process Explorer are working will a poll mechanism (can be configured through View - Update Speed).
So i would say there exists no such mechanism and all you can do is regularly retrieve the list of all running process and compare it with the list you retrieved beforehand.

How to get cpu usage % in C# for each running process

I use System.Diagnostics.Process.GetProcesses() to get process list.
I found TotalProcessorTime property for each process - it is TimeSpan.
But how to get relative values of CPU usage, i. e. i need % of total CPU usage for each running process.
use WQL (queries WMI like SQL)
see attached link for few samples:WQL
Win32_PerfFormattedData_PerfProc_Process is your class for getting CPU data.
I found a workaround decision see this question
The plan is as follows: take counters data in stndard way, then process log files.
Realtime monitoring is not critical.

Categories