text
stringlengths
0
13.4k
waiting for the calling thread to complete or release a held resource.
GoalKicker.com – .NET Framework Notes for Professionals
126
Chapter 40: Process and Thread anity
setting
Parameter
affinity
Details
integer that describes the set of processors on which the process is allowed to run. For example, on a 8
processor system if you want your process to be executed only on processors 3 and 4 than you choose
affinity like this : 00001100 which equals 12
Section 40.1: Get process anity mask
public static int GetProcessAffinityMask(string processName = null)
{
Process myProcess = GetProcessByName(ref processName);
int processorAffinity = (int)myProcess.ProcessorAffinity;
Console.WriteLine("Process {0} Affinity Mask is : {1}", processName,
FormatAffinity(processorAffinity));
return processorAffinity;
}
public static Process GetProcessByName(ref string processName)
{
Process myProcess;
if (string.IsNullOrEmpty(processName))
{
myProcess = Process.GetCurrentProcess();
processName = myProcess.ProcessName;
}
else
{
Process[] processList = Process.GetProcessesByName(processName);
myProcess = processList[0];
}
return myProcess;
}
private static string FormatAffinity(int affinity)
{
return Convert.ToString(affinity, 2).PadLeft(Environment.ProcessorCount, '0');
}
}
Example of usage :
private static void Main(string[] args)
{
GetProcessAffinityMask();
Console.ReadKey();
}
// Output:
// Process Test.vshost Affinity Mask is : 11111111
Section 40.2: Set process anity mask
public static void SetProcessAffinityMask(int affinity, string processName = null)
GoalKicker.com – .NET Framework Notes for Professionals
127
{
Process myProcess = GetProcessByName(ref processName);
Console.WriteLine("Process {0} Old Affinity Mask is : {1}", processName,
FormatAffinity((int)myProcess.ProcessorAffinity));
myProcess.ProcessorAffinity = new IntPtr(affinity);
Console.WriteLine("Process {0} New Affinity Mask is : {1}", processName,
FormatAffinity((int)myProcess.ProcessorAffinity));
}
Example of usage :
private static void Main(string[] args)
{
int newAffinity = Convert.ToInt32("10101010", 2);
SetProcessAffinityMask(newAffinity);
Console.ReadKey();
}
// Output :
// Process Test.vshost Old Affinity Mask is : 11111111
// Process Test.vshost New Affinity Mask is : 10101010
GoalKicker.com – .NET Framework Notes for Professionals
128