Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8977091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:17:37+00:00 2026-06-15T19:17:37+00:00

I have created a window application in C#.Now I want to set the CPU

  • 0

I have created a window application in C#.Now I want to set the CPU affinity for this application.I may have 2 processors,4 processors,8 processors or may be more than 8 processors.

I want to set the cpu affinity using input from interface.

How can i achieve this? How can it is possible to set the affinity using Environment.ProcessorCount?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:17:38+00:00Added an answer on June 15, 2026 at 7:17 pm

    Try this:

    Process.GetCurrentProcess().ProcessorAffinity = (System.IntPtr)2;
    

    Here‘s more about it.

    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
    

    Here’s a small sample program:

    //TODO: manage exceptions
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Total # of processors: {0}", Environment.ProcessorCount);
            Console.WriteLine("Current processor affinity: {0}", Process.GetCurrentProcess().ProcessorAffinity);
            Console.WriteLine("*********************************");
            Console.WriteLine("Insert your selected processors, separated by comma (first CPU index is 1):");
            var input = Console.ReadLine();
            Console.WriteLine("*********************************");
            var usedProcessors = input.Split(',');
    
            //TODO: validate input
            int newAffinity = 0;
            foreach (var item in usedProcessors)
            {
                newAffinity = newAffinity | int.Parse(item);
                Console.WriteLine("Processor #{0} was selected for affinity.", item);
            }
            Process.GetCurrentProcess().ProcessorAffinity = (System.IntPtr)newAffinity;
            Console.WriteLine("*********************************");
            Console.WriteLine("Current processor affinity is {0}", Process.GetCurrentProcess().ProcessorAffinity);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created Windows Application. In this, I have multiple tables in dataset, now
So me and a friend have created an application using Eclipse RCP. Now we
I have created a windows mobile application in Visual Studio. I want to templatize
Im using c# .net windows application form. I have created many databases with many
I have a Microsoft Access 2010 database(*). Now, using Visual Studio 2010, I want
I have created two java class with UIBinder in GWT application. I want to
I have created by custom UIButton for iPad application. Now for specific reasons, I
I have this batch file: @echo off echo StrText=Application created Successfully > spk.vbs echo
i have created simple windows application. when it runs i set its Visible property
I have created a data table using disconnected class, also created the data set,xml

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.