4

I have installed NCrunch which is a tool for (among other things) parallel tests execution. It provides two settings MaxNumberOfUsedCores and MaxNumberOfUsedThreads. The first setting looks intriguing. I don't remember any .NET means which would allow to control cores which are used to execute your code. So the question is how to do it?

SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
  • 2
    maybe -> http://stackoverflow.com/questions/2823076/forcing-an-app-to-run-single-core-only and http://stackoverflow.com/questions/3429999/force-c-sharp-application-to-use-a-single-core-in-a-pc-with-a-multicore-processo – Sebastian Piu Jun 24 '12 at 18:19

1 Answers1

1

Have you tried ProcessThread.ProcessorAffinity? ProcessorAffinity represents each processor as a bit. Bit 0 represents processor one, bit 1 represents processor two, and so on. For example:

var currectProcess = System.Diagnostics.Process.GetCurrentProcess();

foreach(System.Diagnostics.ProcessThread thread in currectProcess.Threads) 
{ 
    // this could give you something similar to MaxNumberOfUsedCores 
    thread.ProcessorAffinity = (IntPtr)0x0007; // Valid processors: 1, 2, or 3
}

Now I'm having following configuration at NCrunch, that is looking quite similar to previous c# sample:

  • CPU cores assigned to NCrunch: 0, 1, 2
  • CPU cores assigned to Visual Studio: 3

But only author of NCrunch @remco-mulder could tell us, is it true or not.

btw: ReSharper has similar options for controlling number of parallel running threads of unittests.

Akim
  • 8,469
  • 2
  • 31
  • 49