ProcessThread.ProcessorAffinity Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Sets the processors on which the associated thread can run.

Namespace:   System.Diagnostics
Assembly:  System (in System.dll)

public IntPtr ProcessorAffinity { set; }

Property Value

Type: System.IntPtr

An IntPtr that points to a set of bits, each of which represents a processor that the thread can run on.

Exception Condition

The processor affinity could not be set.


The platform is Windows 98 or Windows Millennium Edition.


The process is on a remote computer.

The processor affinity of a thread is the set of processors it has a relationship to. In other words, those it can be scheduled to run on.

ProcessorAffinity represents each processor as a bit. Bit 0 represents processor one, bit 1 represents processor two, and so on. The following table shows a subset of the possible ProcessorAffinity for a four-processor system.

Property value (in hexadecimal)

Valid processors






1 or 2




1 or 3


1, 2, or 3


1, 2, 3, or 4

You can also specify the single, preferred processor for a thread by setting the IdealProcessor property. 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.

The following example shows how to set the ProcessorAffinity 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)
            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;

.NET Framework
Available since 1.1
Return to top