Opc.Ua.Configuration.ServiceManager.SetServiceProcessorAffinity C# (CSharp) Method

SetServiceProcessorAffinity() public static method

Set the processor affinity for the service with the given name.
If the system has 2 processor and the service is running on processor 2 the affinity bit mask will be : [true][false] If the system has 2 processor and the service is running on both processors the affinity bit mask will be : [true][true]
public static SetServiceProcessorAffinity ( string serviceName, bool affinity ) : bool
serviceName string the service name.
affinity bool The affinity bitmask.
return bool
        public static bool SetServiceProcessorAffinity(string serviceName, bool[] affinity)
        {
            Service service = GetService(serviceName);
            if (service != null && service.ProcessId > 0 && affinity != null && affinity.Length > 0)
            {
                try
                {
                    System.Diagnostics.Process process = System.Diagnostics.Process.GetProcessById(service.ProcessId);
                    int affinityInt = affinity[affinity.Length - 1] ? 1 : 0;
                    for (int i = affinity.Length - 2; i >= 0; i--)
                    {
                        if (affinity[i])
                        {
                            affinityInt = affinityInt + 2 * (affinity.Length - 1 - i);
                        }
                    }
                    IntPtr affinityPtr = new IntPtr(affinityInt);
                    process.ProcessorAffinity = affinityPtr;
                    return true;
                }
                catch { }
            }
            return false;
        }