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)?

3
  • possible duplicate of Limit number of processors used in ThreadPool Commented Jun 25, 2014 at 12:02
  • 1
    I'd love to meet the person who write that requirement so i could ask him/her what on earth it is for. Commented Jun 25, 2014 at 12:04
  • 1
    @Gusdor Some old apps crash when run on multiple cores. That is why I came here. Commented Nov 5, 2018 at 13:17

1 Answer 1

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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.