更改线程的优先级,使我的程序和电脑反应更灵敏优先级、使我、线程、灵敏

2023-09-03 12:49:30 作者:彼岸花開開彼岸

我写了一个.NET的WinForms应用程序,使用一个辅助线程做一些繁重的处理,通信,它的进步回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.

我假设我需要降低处理线程的优先级,和/或增加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.

难道只是简单的follwing?还是有别的东西,我应该怎么办?

Is it just as easy as the follwing? Or is there something else I should do?

Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;

我应该如何改变的重点是什么?我应减少的处理的优先级,或增加的UI,或二者的优先?到什么设置? 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?

推荐答案

我不认为一定线程优先级是你的问题(尽管它可能是它的一部分)。看一看这太问题:Background工作线程的优先级。

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.

这可能是过于紧密的循环在你的后台线程这是保持CPU时间上线。有几种方法,从残酷的(线程休眠),以更合理的(mutexs和事件)。

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.