更改线程优先级以使我的程序和计算机响应更快

发布于 2024-08-21 00:28:07 字数 416 浏览 14 评论 0原文

我编写了一个 .NET winforms 应用程序,它使用辅助线程进行一些繁重的处理,将其进度传达回 UI 线程。一切正常,表单显示进度,我还创建了一个取消按钮来中断处理线程。然而,当耗时的过程开始时,应用程序和我的整个计算机都会变慢。拖动窗口需要很长时间,甚至在尝试在记事本中输入字母时也会出现明显的延迟。

我假设我需要降低处理线程的优先级,和/或提高 UI 线程的优先级。这是对的吗?现在两个线程都是普通优先级。

是不是像下面这样简单呢?或者还有什么我应该做的吗?

Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;

我应该如何改变优先级?我应该降低处理的优先级,还是提高 UI 的优先级,或者两者兼而有之?以及什么设置?高于正常水平,还是最高?

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.

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.

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?

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

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

发布评论

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

评论(4

心欲静而疯不止 2024-08-28 00:28:07

我不一定认为线程优先级是您的问题(尽管它可能是其中的一部分)。看看这个问题:后台工作线程优先级

后台线程中的循环可能过于紧密,从而导致该线程占用了 CPU 时间。有多种方法可以解决这个问题,从残酷的(线程睡眠)到更合理的(互斥体和事件)。

您还可以尝试分析后台线程(直接或在测试工具中)以查看其大部分时间都花在哪里,并尝试使用异步事件或类似的卸载技术来隔离它。

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.

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

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.

镜花水月 2024-08-28 00:28:07

如果您希望后台线程不影响整个系统的响应能力,则需要降低其优先级,最有可能的方法是将其优先级设置为 低于正常

否则,它将具有您当前看到的相同效果。

话虽这么说,我会犹豫是否要在自己的代码中执行此操作。如果您的程序在具有更多处理核心的系统上运行,这可能不会成为问题,并且降低线程优先级(可能)会导致您的算法需要更长的时间来处理。

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.

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.

楠木可依 2024-08-28 00:28:07

您通常希望保留主线程的优先级,并将处理线程的优先级降低到空闲。

You generally want to leave the priority of your main thread alone, and reduce the priority of the processing thread to Idle.

爱的故事 2024-08-28 00:28:07

通常,您应该将工作线程的优先级设置为一个很好的级别(例如,用户可能想要在另一个应用程序中执行某些操作,甚至工作线程应该发挥良好的作用),即使 Windows 已经提升了“活动”进程线程(应用程序)你有一个带有输入焦点的窗口)一点点,这样它会感觉反应更快。当您需要满足一些时间限制时,通常需要更高的优先级。

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.

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