Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Number of processors

Number of processors

Scheduled Pinned Locked Moved C#
questiontutorial
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jay Gatsby
    wrote on last edited by
    #1

    Hello, I have a quick question. Does anybody know how to find out how many processors are on the machine my app is running on? I've been drudging through System.Diagnostics to no avail, and I was wondering if anyone could point me in the right direction. Certainly there must be a constant or a property with a get accessor somewhere... but I just can't seem to find it. Thank you.

    -Gatsby

    O M L H 4 Replies Last reply
    0
    • J Jay Gatsby

      Hello, I have a quick question. Does anybody know how to find out how many processors are on the machine my app is running on? I've been drudging through System.Diagnostics to no avail, and I was wondering if anyone could point me in the right direction. Certainly there must be a constant or a property with a get accessor somewhere... but I just can't seem to find it. Thank you.

      -Gatsby

      O Offline
      O Offline
      originSH
      wrote on last edited by
      #2

      WMI has a wealth of hardware information. I'm not sure if theres a direct .Net class with the info in it.

      O 1 Reply Last reply
      0
      • J Jay Gatsby

        Hello, I have a quick question. Does anybody know how to find out how many processors are on the machine my app is running on? I've been drudging through System.Diagnostics to no avail, and I was wondering if anyone could point me in the right direction. Certainly there must be a constant or a property with a get accessor somewhere... but I just can't seem to find it. Thank you.

        -Gatsby

        M Offline
        M Offline
        Mustafa Ismail Mustafa
        wrote on last edited by
        #3

        Use WMI. Try the following: string cpuInfo = String.Empty; string temp = String.Empty; ManagementClass mc = new ManagementClass("Win32_Processor"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if (cpuInfo == String.Empty) { cpuInfo = mo.Properties["ProcessorId"].Value.ToString(); } } Console.WriteLine(cpuInfo); Console.ReadLine(); Don't forget to include the System.Management Namespace. Mustafa Ismail There are 10 kinds of people in this world. Those who understand binary and those who don't... ______________________ "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

        1 Reply Last reply
        0
        • J Jay Gatsby

          Hello, I have a quick question. Does anybody know how to find out how many processors are on the machine my app is running on? I've been drudging through System.Diagnostics to no avail, and I was wondering if anyone could point me in the right direction. Certainly there must be a constant or a property with a get accessor somewhere... but I just can't seem to find it. Thank you.

          -Gatsby

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, that is a complex question, and I don't have the complete answer. What do you call a processor (there is some disagreement): - there is hyperthreading (basically 2 or more processor states sharing all other processor resources: functional units, caches, bus interface, ...), which you can disable through BIOS - there is multi-processing on a chip - there is multi-chip packages - and finally visible multi-processing: more than one IC package And obviously these can be combined. That's the technical view. Then there is the matter of software licensing, which exists in many forms, such as per processor, per system, per seat. There is room for debate! Environment.ProcessorCount "Gets the number of processors on the current machine" whatever that means. I expect/hope it to mean the largest number you can obtain taking into account all the forementioned possibilities. Also Process.ProcessorAffinity should have the value 2^n - 1 when n=Environment.ProcessorCount Hope this helps

          Luc Pattyn [Forum Guidelines] [My Articles]


          this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


          J 1 Reply Last reply
          0
          • J Jay Gatsby

            Hello, I have a quick question. Does anybody know how to find out how many processors are on the machine my app is running on? I've been drudging through System.Diagnostics to no avail, and I was wondering if anyone could point me in the right direction. Certainly there must be a constant or a property with a get accessor somewhere... but I just can't seem to find it. Thank you.

            -Gatsby

            H Offline
            H Offline
            Hessam Jalali
            wrote on last edited by
            #5

            System.Environment.ProcessorCount will give you number of them (actually in some cases will return number of cores not processors just like DeviceManager!) but if you need to know how many processors are on the machine really you can use WMI to found out and it would be somewhat like this

                static int ProcessorCount()
                {
                    ManagementObjectSearcher mos = new ManagementObjectSearcher(new SelectQuery("Win32\_Processor"));
            
                    ManagementObjectCollection moc = mos.Get();
            
                    List processorIds = new List();
            
                    foreach (ManagementObject mo in moc)
                    {
                        string pID = mo.Properties\["ProcessorId"\].Value as string;
                        if (!processorIds.Contains(pID))
                            processorIds.Add(pID);
                    }
            
                    return processorIds.Count;
                }
            

            in the code, processorIds were checked and because the cores from the same processor have identical processorIds they're not gonna be counted twice or more hope the post would be useful good luck

            J 1 Reply Last reply
            0
            • H Hessam Jalali

              System.Environment.ProcessorCount will give you number of them (actually in some cases will return number of cores not processors just like DeviceManager!) but if you need to know how many processors are on the machine really you can use WMI to found out and it would be somewhat like this

                  static int ProcessorCount()
                  {
                      ManagementObjectSearcher mos = new ManagementObjectSearcher(new SelectQuery("Win32\_Processor"));
              
                      ManagementObjectCollection moc = mos.Get();
              
                      List processorIds = new List();
              
                      foreach (ManagementObject mo in moc)
                      {
                          string pID = mo.Properties\["ProcessorId"\].Value as string;
                          if (!processorIds.Contains(pID))
                              processorIds.Add(pID);
                      }
              
                      return processorIds.Count;
                  }
              

              in the code, processorIds were checked and because the cores from the same processor have identical processorIds they're not gonna be counted twice or more hope the post would be useful good luck

              J Offline
              J Offline
              Jay Gatsby
              wrote on last edited by
              #6

              Quite helpful! Problem solved. Thank you.

              -Gatsby

              1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, that is a complex question, and I don't have the complete answer. What do you call a processor (there is some disagreement): - there is hyperthreading (basically 2 or more processor states sharing all other processor resources: functional units, caches, bus interface, ...), which you can disable through BIOS - there is multi-processing on a chip - there is multi-chip packages - and finally visible multi-processing: more than one IC package And obviously these can be combined. That's the technical view. Then there is the matter of software licensing, which exists in many forms, such as per processor, per system, per seat. There is room for debate! Environment.ProcessorCount "Gets the number of processors on the current machine" whatever that means. I expect/hope it to mean the largest number you can obtain taking into account all the forementioned possibilities. Also Process.ProcessorAffinity should have the value 2^n - 1 when n=Environment.ProcessorCount Hope this helps

                Luc Pattyn [Forum Guidelines] [My Articles]


                this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


                J Offline
                J Offline
                Jay Gatsby
                wrote on last edited by
                #7

                That does help, quite alot! Combined with the other replies, I'm all set. Thanks!

                -Gatsby

                1 Reply Last reply
                0
                • O originSH

                  WMI has a wealth of hardware information. I'm not sure if theres a direct .Net class with the info in it.

                  O Offline
                  O Offline
                  originSH
                  wrote on last edited by
                  #8

                  If your going to 1 vote atleast explain why, and how the post could be improved.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups