views:

1867

answers:

3

Hello everyone,

I have a program written in C#, I am using VSTS 2008 + .Net 3.5 + Windows Vista Enterprise x86 to develop a Windows Forms application.

My current computer is dual-core CPU, I want to set CPU affinity of my program to run on a specific CPU and free another CPU to do some other job. Any ideas how to do this? Either through coding or configuration is ok.

A little more background is, my program is CPU intensive, so I do not want to let it occupy all two CPU resources on my computer and I want to free one CPU so that I can browse network at the same time quickly. :-)

thanks in advance, George

+3  A: 

Actually, your application will not use more than one CPU unless you specifically do something to utilize more CPUs. If you use the thread pool and/or start additional threads you may use additional available cores, but otherwise your application will just have one thread per default and thus only use one CPU.

Brian Rasmussen
My application will use more than one CPU because it recording through Windows Media Encoder and do real time encoding to wmv format -- I have tested it and seen all CPU resources are occupied from Windows task manager.Any ideas how to set CPU affinity?
George2
@Brian: It may still make sense to force a thread to remain on one core, for those systems where the cache is per-core.
mghie
@mghie, I agree with you. Any ideas for my problem?
George2
@George2: Well, see my answer, read the MSDN documentation how to use the functions, and call them using platform invoke.
mghie
Cool, @mghie, please see my reply, I like your answer.
George2
+7  A: 
  1. Go to Task Manager -> Processes tab.
  2. Look for your program. Right click on it.
  3. Select Set Affinity and uncheck one of the checkboxes.

This should free up one processor for you.

For doing it from the code you can add this statement:

System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity = (System.IntPtr) 1;

Cheers!

Gaurav
Any programming or configuration ways? I do not want end user to do this each time. :-)
George2
Try this:System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity = 1;
Gaurav
BTW: To use second set it to 2 and to use both cores you can set the value to 3 :)
Gaurav
System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity = (System.IntPtr)1;forgot the typecasting laste time :P
Gaurav
@Gaurav: If you add this to the answer I'll upvote it.
mghie
@mghie: done :-)
Gaurav
@Gaurav: Done :-)
mghie
@Gaurav, why use IntPrt, why not use int?
George2
+1  A: 

The Windows API functions to do this are SetProcessAffinityMask() and SetThreadAffinityMask(). I don't know .NET so I can't say whether there are wrappers around those functions, but this seems to suggest otherwise.

BTW: I agree that these are necessary only in very specific circumstances, it's normally best to let the OS scheduler deal with it. It's one of those questions where you probably shouldn't do it if you have to ask how.

mghie
@mghie, in my problem, 1. I think I should call SetProcessAffinityMask other than SetThreadAffinityMask, correct? 2. Could I call SetProcessAffinityMask from the current process to set the affinity of the current process? Or I have to set it up from external tool?
George2
You can do it from the process itself. It's probably best to start with this MSDN article: http://msdn.microsoft.com/en-us/magazine/cc300701.aspx#S11, it should provide some code as well as helpful background information.
mghie
I like your reply, about using System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity property, why should we use IntPrt, why not use int?
George2