C# 更改线程优先级以使我的程序和计算机响应更快
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2231881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing thread priority to make my program and computer more responsive
提问by Eric Anastas
I've written a .NET winforms application that uses a secondary thread to do some heavy processing, which communicates it's progress back to the UI thread. Everything works correctly, the form shows the progress, and I've also created a cancel button to interrupt the processing thread. However, when the time consuming process is going the application and my entire computer slows way down. It takes a long time drag windows around, and there is even a significant delay when trying to type letters into notepad.
我已经编写了一个 .NET winforms 应用程序,它使用一个辅助线程来进行一些繁重的处理,将它的进度传达回 UI 线程。一切正常,表单显示进度,我还创建了一个取消按钮来中断处理线程。但是,当耗时的过程进行应用程序时,我的整个计算机就会变慢。拖动窗口需要很长时间,甚至在尝试将字母输入记事本时会出现明显延迟。
I'm assuming I need to reduce the priority of the processing thread, and/or increase the priority of the UI thread. Is this right? Right now both threads are Normal priority.
我假设我需要降低处理线程的优先级,和/或增加 UI 线程的优先级。这是正确的吗?现在两个线程都是普通优先级。
Is it just as easy as the follwing? Or is there something else I should do?
是不是像下面一样容易?或者还有什么我应该做的吗?
Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;
How should I change the priorities? Should I reduce the priority of the processing, or increase the priority of the UI, or both? And to what setting? AboveNormal, or highest?
我应该如何更改优先级?我应该降低处理的优先级,还是提高 UI 的优先级,还是两者兼而有之?和什么设置?高于正常,还是最高?
回答by Jerry Coffin
You generally want to leave the priority of your main thread alone, and reduce the priority of the processing thread to Idle.
您通常希望单独保留主线程的优先级,并将处理线程的优先级降低到 Idle。
回答by Reed Copsey
If you want the background thread to not effect the system responsiveness as a whole, you'll need to lower it's priority, most likely by setting it's Priority to BelowNormal.
如果您希望后台线程不影响整个系统的响应能力,您需要降低它的优先级,最有可能的方法是将它的 Priority 设置为BeyondNormal。
Otherwise, it will have the same effect you're currently seeing.
否则,它将产生与您目前看到的相同的效果。
That being said, I'd be hesitant to do this in my own code. If your program is run on a system with more processing cores, this will likely not be an issue, and lowering the thread priority (potentially) will cause your algorithm to take longer to process.
话虽如此,我会犹豫在我自己的代码中执行此操作。如果您的程序在具有更多处理核心的系统上运行,这可能不是问题,并且降低线程优先级(可能)会导致您的算法需要更长的时间来处理。
回答by GrayWizardx
I dont necessarily think thread priority is your issue (though it could be part of it). Take a look at this SO question: Background Worker Thread Priority.
我不一定认为线程优先级是您的问题(尽管它可能是其中的一部分)。看看这个 SO 问题:Background Worker Thread Priority。
It is probably overly tight loops in your background thread which are keeping cpu time on that thread. There are several ways to fix it from the brutal (thread sleeps) to the more reasonable (mutexs and events).
可能是后台线程中的循环过于紧凑,从而使该线程上的 CPU 时间保持不变。有几种方法可以修复它,从残酷的(线程休眠)到更合理的(互斥体和事件)。
You can also try profiling the background thread (either directly, or in a test harness) to see where it is spending the majority of its time, and try to isolate this with async events or similar offloading techniques.
您还可以尝试分析后台线程(直接或在测试工具中)以查看它在何处花费大部分时间,并尝试使用异步事件或类似的卸载技术将其隔离。
回答by Ritsaert Hornstra
Normally you should set the priority of the worker thread to a nice level (eg the user might want to do someing in another application and even them the worker thread should play nice) even though Windows already boosts the "active" processes thread (the application you have a window with input focus) a little bit so it will feel more responsive. The higher priorities are usually needed when you need to meet some time constraints.
通常你应该将工作线程的优先级设置为一个很好的级别(例如,用户可能想要在另一个应用程序中做一些事情,即使他们工作线程也应该玩得很好),即使 Windows 已经提升了“活动”进程线程(应用程序你有一个带有输入焦点的窗口)一点点,所以它会感觉更灵敏。当您需要满足一些时间限制时,通常需要更高的优先级。