C# 如何限制进程使用的核心数?
我想让我的程序打开一些其他进程,项目的一个要求是,每个打开的进程只能在单个核心上运行 我知道可以使用processorAffinity选择特定的处理器,但是否可以设置最大内核数(在我的案例1中)?您必须尝试使用C# 如何限制进程使用的核心数?,c#,process,cpu-cores,C#,Process,Cpu Cores,我想让我的程序打开一些其他进程,项目的一个要求是,每个打开的进程只能在单个核心上运行 我知道可以使用processorAffinity选择特定的处理器,但是否可以设置最大内核数(在我的案例1中)?您必须尝试使用 可能是重复的,我很想见到写这个要求的人,这样我就可以问他/她这到底是为了什么。@Gusdor一些旧的应用程序在多核上运行时会崩溃。这就是我来这里的原因。 using System; using System.Diagnostics; namespace ProcessThreadIde
可能是重复的,我很想见到写这个要求的人,这样我就可以问他/她这到底是为了什么。@Gusdor一些旧的应用程序在多核上运行时会崩溃。这就是我来这里的原因。
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;
}
}
}