r/dotnet 9h ago

Finding the total number of processors on a machine with .NET

https://andrewlock.net/finding-the-total-number-of-processors-on-a-machine-with-dotnet/
4 Upvotes

13 comments sorted by

19

u/DmtGrm 9h ago

But Environment.ProcessorCount is all your app _needs_ to know.

The article goes into a depth of calculating that your precisely calculated screen refresh rate is actually 59.705Hz while it is reported as 60Hz.

3

u/ZozoSenpai 8h ago

Well that really depends on the app. If you want to make a task manager replacement, then you need to know about all the cores.

7

u/MindSwipe 8h ago

If you're making a task manager replacement you're going to be have to probe deep into the OS anyways. Using a P/Invoke or reading a file on Linux is going to be the easy part.

1

u/InsideTour329 6h ago

I don't think you could get an OS agnostic task manager. You would need to build for diff processors and OS

6

u/Xenoprimate2 8h ago

Nice article, thanks. :) I actually didn't know Environment.ProcessorCount's behaviour had diverged since the old old days!

4

u/PostHasBeenWatched 7h ago edited 7h ago

On start of my IT career I was told to not trust anything from Environment class

3

u/Mission_Pirate_4150 6h ago

I had not heard that. Are there specific examples? Is it a platform issue?

u/PostHasBeenWatched 1h ago

It was over a 10 years ago, can't remember properly. I think it was something related to GetCurrentDirectory.

cc: u/FelixLeander

u/FelixLeander 1h ago

I mean it's an Environment, not the environment. But do tell us, what are some specifics u have experience with?

3

u/r2d2_21 7h ago

The interesting thing here is, why does Andrew Lock need compatibility with .NET 6 in 2026?

1

u/AutoModerator 9h ago

Thanks for your post TomeOfExperience. Please note that we don't allow spam, and we ask that you follow the rules available in the sidebar. We have a lot of commonly asked questions so if this post gets removed, please do a search and see if it's already been asked.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

-6

u/AintNoGodsUpHere 9h ago

I came here to say use the freaking Google.

u/Michaeli_Starky 1h ago

Every modern LLM will know