Condividi tramite


ProcessThread.ProcessorAffinity Proprietà

Definizione

Imposta i processori in cui il thread associato può essere eseguito.

public:
 property IntPtr ProcessorAffinity {  void set(IntPtr value); };
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public IntPtr ProcessorAffinity { set; }
public IntPtr ProcessorAffinity { set; }
[System.ComponentModel.Browsable(false)]
public IntPtr ProcessorAffinity { set; }
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
member this.ProcessorAffinity : nativeint
member this.ProcessorAffinity : nativeint
[<System.ComponentModel.Browsable(false)>]
member this.ProcessorAffinity : nativeint
Public Property ProcessorAffinity As IntPtr

Valore della proprietà

IntPtr

nativeint

Oggetto IntPtr che punta a un set di bit, ognuno dei quali rappresenta un processore su cui il thread può essere eseguito.

Attributi

Eccezioni

Impossibile impostare l'affinità del processore.

Il processo si trova in un computer remoto.

Esempio

Nell'esempio seguente viene illustrato come impostare la ProcessorAffinity proprietà per un'istanza del Blocco note sul primo processore.

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

Commenti

L'affinità del processore di un thread è il set di processori a cui ha una relazione. In altre parole, è possibile pianificare l'esecuzione.

ProcessorAffinity rappresenta ogni processore come bit. Bit 0 rappresenta il processore 1, bit 1 rappresenta il processore due e così via. La tabella seguente illustra un sottoinsieme del possibile ProcessorAffinity per un sistema a quattro processori.

Valore della proprietà (in formato esadecimale) Processori validi
0x0001 1
0x0002 2
0x0003 1 o 2
0x0004 3
0x0005 1 o 3
0x0007 1, 2 o 3
0x000F 1, 2, 3 o 4

È anche possibile specificare il singolo processore preferito per un thread impostando la IdealProcessor proprietà . Un thread di processo può eseguire la migrazione dal processore al processore, con ogni migrazione ricaricando la cache del processore. La specifica di un processore per un thread può migliorare le prestazioni in carichi di sistema elevati riducendo il numero di volte in cui viene ricaricata la cache del processore.

Si applica a

Vedi anche