ProcessThread.IdealProcessor 屬性
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
設定執行這個執行緒的慣用處理器。
public:
property int IdealProcessor { void set(int value); };
C#
public int IdealProcessor { set; }
C#
[System.ComponentModel.Browsable(false)]
public int IdealProcessor { set; }
member this.IdealProcessor : int
[<System.ComponentModel.Browsable(false)>]
member this.IdealProcessor : int
Public Property IdealProcessor As Integer
執行緒的慣用處理器,用於系統排程執行緒時,以判斷執行執行緒所在的處理器。
- 屬性
系統無法設定在指定的處理器上啟動執行緒。
處理序位於遠端電腦上。
下列範例示範如何將記事本實例的 屬性設定 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;
}
}
}
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 是以零起始。 換句話說,若要設定第一個處理器的線程親和性,請將 屬性設定為零。
系統會盡可能在其慣用處理器上排程線程。
進程線程可以從處理器移轉至處理器,每個移轉都會重載處理器快取。 指定線程的處理器可以藉由減少處理器快取重載的次數,來改善大量系統負載下的效能。
產品 | 版本 |
---|---|
.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 |