2014-06-25 42 views
1

我希望我的程序打開一些其他進程,並且項目的其中一個要求是每個進程只能在單個核心上運行。如何限制進程使用的內核數量?

我知道一個特定的處理器可以選擇processorAffinity,但可以設置最大數量的核心(在我的情況下是1)?

+0

可能重複http://stackoverflow.com/questions/11432084/limit-number-of-processors-used -in-threadpool) –

+1

我很想見到寫這個需求的人,所以我可以問他/她究竟是什麼。 – Gusdor

回答

0

你必須嘗試用ProcessThread.ProcessorAffinity

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; 
     } 
    } 
} 
[限於處理器的數量在線程池使用(的
+0

我希望有一個更優雅的解決方案,但thx。 – Binyamin

相關問題