强制 C# 应用程序在具有多核处理器的 PC 中使用单核

发布于 2024-09-12 23:02:57 字数 182 浏览 14 评论 0原文

我在我的 C# 应用程序中使用 Haptek People Putty 播放器,我在论坛上看到人们说它不能很好地与多核处理器配合使用。我的应用程序在我的 Core 2 Duo 笔记本电脑上运行良好,但当我尝试在四核台式机上运行它时,它会滞后很多。我正在考虑自己调查这个问题,在这种情况下,我将不得不强制我的应用程序在单核上运行。这在 C# 中可能吗?

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#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风轻花落早 2024-09-19 23:02:57

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

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

IdealProcessor 是单核的从零开始的标识,ProcessorAffinity 是一个位掩码,因此 1 允许核心为零,2 允许。核心1,3允许核心0和1,4允许核心2,等等

我会彻底测试,这实际上会损害你的性能,降低制作能力。使用不同的核心,这毕竟通常是一个优势。

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.

旧情别恋 2024-09-19 23:02:57

如果应用程序是单线程的,它将无法利用多核。但是,内核可能会在内核之间碰撞线程。我怀疑这就是你的性能问题的原因。

如果您想将线程绑定到单个核心(不确定是否可以保证),您可能需要检查 System.Diagnostics.ProcessThread.ProcessorAffinity 属性,尽管我从未这样做过我自己用过。

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.

快乐很简单 2024-09-19 23:02:57
  • 在 C# 中实际上不可能。好吧,我不可能知道。您需要互操作,这样它就可以工作。

  • 你使用多线程吗?如果没有 - 嗯 - 抱歉 - 你能做的不多。标准 UI 应用程序无论如何都不使用多核。

基本上,不使用线程(工作项使用线程)的应用程序本质上是单核。

  • 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文