ProcessThread.IdealProcessor 屬性

定義

設定執行這個執行緒的慣用處理器。Sets the preferred processor for this thread to run on.

public:
 property int IdealProcessor {  void set(int value); };
[System.ComponentModel.Browsable(false)]
public int IdealProcessor { set; }
member this.IdealProcessor : int
Public Property IdealProcessor As Integer

屬性值

執行緒的慣用處理器,用於系統排程執行緒時,以判斷執行執行緒所在的處理器。The preferred processor for the thread, used when the system schedules threads, to determine which processor to run the thread on.

屬性

例外狀況

系統無法設定在指定的處理器上啟動執行緒。The system could not set the thread to start on the specified processor.

處理序位於遠端電腦上。The process is on a remote computer.

範例

下列範例示範如何將「記事本」 IdealProcessor實例的屬性設定為第一個處理器。The following example demonstrates how to set the IdealProcessor property for an instance of Notepad to the first processor.

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;
        }
    }
}
Imports System.Diagnostics



Class Program
    
    Shared Sub Main(ByVal args() As String) 
        ' Make sure there is an instance of notepad running.
        Dim notepads As Process() = Process.GetProcessesByName("notepad")
        If notepads.Length = 0 Then
            Process.Start("notepad")
        End If
        Dim threads As ProcessThreadCollection
        '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 = CType(1, IntPtr)
    End Sub
End Class

備註

IdealProcessor值是以零為基底。The IdealProcessor value is zero-based. 換句話說, 若要為第一個處理器設定執行緒親和性, 請將屬性設定為零。In other words, to set the thread affinity for the first processor, set the property to zero.

系統會盡可能將執行緒排程在其慣用的處理器上。The system schedules threads on their preferred processors whenever possible.

進程執行緒可以從處理器遷移至處理器, 而每個遷移會重載處理器快取。A process thread can migrate from processor to processor, with each migration reloading the processor cache. 指定執行緒的處理器可以藉由減少重載處理器快取的次數, 來改善繁重系統負載下的效能。Specifying a processor for a thread can improve performance under heavy system loads by reducing the number of times the processor cache is reloaded.

適用於