Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing thread priority to make my program and computer more responsive

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?

like image 270
Eric Anastas Avatar asked Feb 09 '10 19:02

Eric Anastas


People also ask

What is the purpose of thread priorities?

In Java, a thread's priority is an integer in the range 1 to 10. The larger the integer, the higher the priority. The thread scheduler uses this integer from each thread to determine which one should be allowed to execute.

Which method is used to change the thread priority?

The setPriority() method of thread class is used to change the thread's priority. Every thread has a priority which is represented by the integer number between 1 to 10. Thread class provides 3 constant properties: public static int MIN_PRIORITY: It is the maximum priority of a thread.

What is thread priority How thread priority are set and changed?

A thread can be created by implementing the Runnable interface and overriding the run() method. Then a Thread object can be created and the start() method called. The thread priority determines when the processor is provided to the thread as well as other resources.

What is thread priority in C++?

Threads provided by the operating system usually have priorities. There is currently no way of setting the priority on a thread with the current C++ thread APIs. However, by using std::thread::native_handle , one can get a handle to the underlying operating system thread and use native APIs for setting priorities.


2 Answers

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.

like image 75
GrayWizardx Avatar answered Sep 21 '22 22:09

GrayWizardx


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.

like image 22
Reed Copsey Avatar answered Sep 22 '22 22:09

Reed Copsey