1

I want my program to open some other processes, and one of the requirements of the project is that each process opened can only be run on a single core.

I know that a specific processor can be picked with processorAffinity but is it possible to set a maximum number of cores (in my case 1)?

Hein Andre Grønnestad
  • 6,885
  • 2
  • 31
  • 43
Binyamin
  • 440
  • 1
  • 6
  • 12

1 Answers1

1

you have to try with ProcessThread.ProcessorAffinity

using System;
using System.Diagnostics;

namespace ProcessThreadIdealProcessor
{
    class Program
    {
        static void Main(string[] args)
        {
            // Make sure there is an instance of notepad running.
            Process[] notepads = Process.GetProcessesByName("notepad");
            if (notepads.Length == 0)
                Process.Start("notepad");
            ProcessThreadCollection threads;
            //Process[] notepads; 
            // Retrieve the Notepad processes.
            notepads = Process.GetProcessesByName("Notepad");
            // Get the ProcessThread collection for the first instance
            threads = notepads[0].Threads;
            // Set the properties on the first ProcessThread in the collection
            threads[0].IdealProcessor = 0;
            threads[0].ProcessorAffinity = (IntPtr)1;
        }
    }
}
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47