ProcessThread.ProcessorAffinity 屬性

定義

設定可以執行相關執行緒的處理器。

C#
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public IntPtr ProcessorAffinity { set; }
C#
public IntPtr ProcessorAffinity { set; }
C#
[System.ComponentModel.Browsable(false)]
public IntPtr ProcessorAffinity { set; }

屬性值

IntPtr

指向一組位元的 IntPtr,其中每個位元表示執行緒可以在其上執行的處理器。

屬性

例外狀況

處理序相似性不能被設定。

處理序位於遠端電腦上。

範例

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

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

備註

線程的處理器親和性是其關聯性的處理器集。 換句話說,它可以排程執行。

ProcessorAffinity 表示每個處理器為位。 位 0 代表處理器 1、位 1 代表處理器二等等。 下表顯示四個處理器系統的可能 ProcessorAffinity 子集。

十六進位) 中的屬性值 ( 有效的處理器
0x0001 1
0x0002 2
0x0003 1 或 2
0x0004 3
0x0005 1 或 3
0x0007 1、2 或 3
0x000F 1、2、3 或 4

您也可以藉由設定 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

另請參閱