Updated: August 2008
Sets the processors on which the associated thread can run.
Namespace:
System.Diagnostics
Assembly:
System (in System.dll)
Visual Basic (Declaration)
<BrowsableAttribute(False)> _
Public WriteOnly Property ProcessorAffinity As IntPtr
Dim instance As ProcessThread
Dim value As IntPtr
instance.ProcessorAffinity = value
[BrowsableAttribute(false)]
public IntPtr ProcessorAffinity { set; }
[BrowsableAttribute(false)]
public:
property IntPtr ProcessorAffinity {
void set (IntPtr value);
}
public function set ProcessorAffinity (value : IntPtr)
Property Value
Type:
System..::.IntPtrAn IntPtr that points to a set of bits, each of which represents a processor that the thread can run on.
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 |
|---|
0x0001 | 1 |
0x0002 | 2 |
0x0003 | 1 or 2 |
0x0004 | 3 |
0x0005 | 1 or 3 |
0x0007 | 1, 2, or 3 |
0x000F | 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.
Imports System
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 'Main
End Class 'Program
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;
}
}
}
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Reference
Date | History | Reason |
|---|
August 2008
| Added an example. |
Customer feedback.
|