在this page中,以下代码是用于更改当前进程的相似性的示例:
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();
}
}
但是对我来说输出是:
ProcessorAffinity:255
ProcessorAffinity:255
表示亲和力没有改变。有什么问题?以及如何更改亲和力?
答案 0 :(得分:1)
就像@ChetanRanpariya在评论中提到的那样,问题是因为您更改了一个过程对象的ProcessorAffinity
(从Process.GetCurrentProcess()
的 second 调用返回)并在其中签入另一个(从Process.GetCurrentProcess()
的第一次调用返回)。这是更正后的示例:
using (var currentProcess = Process.GetCurrentProcess())
{
Console.WriteLine($"ProcessorAffinity: {currentProcess.ProcessorAffinity}");
currentProcess.ProcessorAffinity = (System.IntPtr)3;
Console.WriteLine($"ProcessorAffinity: {currentProcess.ProcessorAffinity}");
}