In this page, the following code is an example for changing the affinity of the current process:
using System;
using System.Diagnostics;
class Program
{
static void Main()
{
Process myProcess = Process.GetCurrentProcess();
Console.WriteLine("ProcessorAffinity: {0}", myProcess.ProcessorAffinity);
Process.GetCurrentProcess().ProcessorAffinity = (System.IntPtr)3;
Console.WriteLine("ProcessorAffinity: {0} ", myProcess.ProcessorAffinity);
Console.ReadKey();
}
}
But the output for me is:
ProcessorAffinity: 255
ProcessorAffinity: 255
meaning that the affinity is not changed. What's the problem? And how can I change the affinity?
As @ChetanRanpariya mension in his comment, the issue is because you changeing ProcessorAffinity
of one process object (returned from the second call of Process.GetCurrentProcess()
) and checking it in another (returned from the first call of Process.GetCurrentProcess()
). Here is corrected sample:
using (var currentProcess = Process.GetCurrentProcess())
{
Console.WriteLine($"ProcessorAffinity: {currentProcess.ProcessorAffinity}");
currentProcess.ProcessorAffinity = (System.IntPtr)3;
Console.WriteLine($"ProcessorAffinity: {currentProcess.ProcessorAffinity}");
}