There may be times when you need to limit the number of processors and threads used by your computer program.
To reduce the number of processors that your program uses, you obtain the current process and set its processor affinity value. For example, say that we have a four-core computer and we want to limit our usage to the first two cores. The binary value for the first two cores is 11, which is 3 in integer form. Now, let's add a method to a new .NET Framework console application and call it AssignCores():
private static void AssignCores(int cores)
{
Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(cores);
}
We pass in an integer to the method. This integer value will be converted into a binary value by .NET Framework. That binary value will use the processors identified by the value of 1. For binary values of 0, the processors will not be used. So, since machine code is...