9

I am using the Haptek People Putty player for my C# application and I've seen people say in the forums that it doesn't work well with a multicore processor. My application runs well on my Core 2 Duo laptop but it lags a lot when I try running it on a Quad Core desktop. I was thinking of investigating this for myself and in that case, I would have to force my application to run on a single core. Is that possible in C#?

peterh
  • 11,875
  • 18
  • 85
  • 108
Paul
  • 1,128
  • 1
  • 11
  • 19
  • 1
    possible duplicate of [How to set processor affinity from Batch File for Windows XP? ](http://stackoverflow.com/questions/827754/how-to-set-processor-affinity-from-batch-file-for-windows-xp) – Hans Passant Aug 07 '10 at 11:12
  • Hmm although it's not the exact same problem, but I could use that as a solution. I will probably have to create a batch file for my application instead of just running it directly. I'll try to see if it works tomorrow. Thanks for the link Hans. – Paul Aug 08 '10 at 04:20

3 Answers3

6

Where a Process variable proc holds the process you care about (Process.GetCurrentProcess() for the running Process, obtaining it from GetProcesses() or GetProcessesByName() etc. for another process. Then:

foreach(ProcessThread pt in proc.Threads)
{
   pt.IdealProcessor = 0;
   pt.ProcessorAffinity = (IntPtr)1;
}

IdealProcessor is a zero-based identity of a single core. ProcessorAffinity is a bitmask, so 1 allows core zero, 2 allows core one, 3 allows cores zero and one, 4 allows core two, and so on.

I would test this thoroughly. Chances are very strong that this will actually damage your performance, in reducing the ability to make use of different cores, which is after-all generally an advantage.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
  • Thanks Jon! I also tried doing this yesterday and it worked for my application. You're right it will hinder performance but it's the only way I could force People Putty to work :( Actually the program works with dual core processors, but since I ran it on a quad core it really slowed down. Now that I got that down to 2, it seems to be working good enough. – Paul Aug 09 '10 at 07:19
  • @akim That edit makes no sense, as then it would refer to the current process which is contrary to the question. – Jon Hanna Jul 26 '12 at 10:45
  • @JonHanna, current snippet will not compile. and it make only first thread to run on single process, but not all threads. – Akim Jul 26 '12 at 10:54
  • @akim I see the actual mistake now, and have fixed it. – Jon Hanna Jul 27 '12 at 08:55
  • This still won't compile: You can't iterate over one Thread, you want to iterate over proc.Threads not proc.Threads[0]or am I missing something? – ASA Oct 01 '14 at 13:03
  • @Traubenfuchs not missing anything except two slightly different thoughts going through my head as I wrote it. Edited. – Jon Hanna Oct 01 '14 at 23:41
  • This solution does not work on Win 10, somehow. No error, no effect. – Michał Woliński Nov 13 '18 at 13:15
2

If the application is single-threaded, it will not take advantage of multiple cores. However, it is possible that the kernel can bump the thread around between cores. I doubt that this is the cause of your performance problems.

If you would like to tie the thread down to a single core (not sure if this can be guaranteed), you might want to check out the System.Diagnostics.ProcessThread.ProcessorAffinity property, although I have never used it myself.

Ani
  • 111,048
  • 26
  • 262
  • 307
1
  • Not really possible IN C#. Well, no way i know off. You need interop, with that it works.

  • Are you using multiple threads? If not - hm - sorry - not a lot you can do. Standard UI applications are not using multiple cores anyway.

Basically, applications not using Threads (Work items use threads) are inherently single core anyway.

kbrimington
  • 25,142
  • 5
  • 62
  • 74
TomTom
  • 61,059
  • 10
  • 88
  • 148
  • My application doesn't use multiple threads. The Haptek People Putty player just provided a DLL so I don't know what it actually does inside. I don't know if they have multiple threads or what not. I guess the problem would lie there since I don't have access to their code :( – Paul Aug 07 '10 at 10:33
  • I would agree on that. Could be the hosting of them. You are declaring STA Threading as the VS templates do? – TomTom Aug 07 '10 at 11:10