ProcessThread.IdealProcessor 屬性

定義

設定執行這個執行緒的慣用處理器。

C#
public int IdealProcessor { set; }
C#
[System.ComponentModel.Browsable(false)]
public int IdealProcessor { set; }

屬性值

執行緒的慣用處理器,用於系統排程執行緒時,以判斷執行執行緒所在的處理器。

屬性

例外狀況

系統無法設定在指定的處理器上啟動執行緒。

處理序位於遠端電腦上。

範例

下列範例示範如何將記事本實例的 屬性設定 IdealProcessor 為第一個處理器。

C#
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;
        }
    }
}

備註

此值 IdealProcessor 是以零起始。 換句話說,若要設定第一個處理器的線程親和性,請將 屬性設定為零。

系統會盡可能在其慣用處理器上排程線程。

進程線程可以從處理器移轉至處理器,每個移轉都會重載處理器快取。 指定線程的處理器可以藉由減少處理器快取重載的次數,來改善大量系統負載下的效能。

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1