强制C#应用程序在具有多核处理器的PC中使用单核 [英] Force C# application to use a single core in a PC with a multicore processor

查看:171
本文介绍了强制C#应用程序在具有多核处理器的PC中使用单核的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,这个问题似乎很奇怪,但是我在C#应用程序中使用的是Haptek People Putty播放器,而且我在论坛上看到有人说它在多核处理器上不能很好地工作。我的应用程序在Core 2 Duo笔记本电脑上可以很好地运行,但是当我尝试在Quad Core台式机上运行它时,它会滞后很多。我当时想为自己进行调查,在这种情况下,我将不得不强制我的应用程序在单个内核上运行。在C#中有可能吗?谢谢!

Hi guys this question might seem weird, but I'm 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#? Thanks!

推荐答案

其中 Process 变量 proc 保存您关心的进程(正在运行的Process的 Process.GetCurrentProcess(),从 GetProcesses()获取它 GetProcessesByName()等用于另一个进程,然后:

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是单个内核的从零开始的标识。ProcessorAffinity是一个位掩码,因此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.

这篇关于强制C#应用程序在具有多核处理器的PC中使用单核的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆