开发者

Changing thread priority to make my program and computer more responsive

开发者 https://www.devze.com 2022-12-19 12:29 出处:网络
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

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 threa开发者_JAVA百科d, 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?


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.


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号