Heavy TPL background threads create delays in WPI UI-Thread

I have a WPF application that uses several background threads to precompile LINQ queries and suppress some values ​​that will be needed later. TPL is used to run these tasks by:

var newTask = new Task(taskAction, myCancelToken, TaskCreationOptions.LongRunning); newTask.Start(); 

This works, tasks are distributed across several processor cores, etc. However, these threads cause a high processor load, which is felt in the user interface, which tends to stumble or even freeze if the threads are not finished.

So, what could be a smart way to smooth the user interface. While researching, I found that he should not give threads special priorities. Others mean that frequent use of Thread.Sleep () is a way that seems a bit exhausted and hacked to me.

Are there any additional ways that I don't know about? Are there real flaws in a priori threads (which is not possible with TPL directly, afaik)?

Thanks in advance!

+4
source share
2 answers

10 threads are too many, say a 4-core machine. If they involve computing, such as pre-compiling queries, they will all dispute processor time and completely abandon the entire machine.

I suggest you use Environment.ProcessorCount to find out how many cores are available, and just start (this number is 1) threads at a time. You can prioritize which work is done first, and the rest of the queues as a continuation.

This will leave the kernel free to serve your UI thread and should respond to the application again.

0
source

Regarding task prioritization, you can do something like http://blogs.microsoft.co.il/blogs/bnaya/archive/2011/01/29/how-to-schedule-task-on-different-thread- priority.aspx

But AFAIK explicitly prioritizing a thread is not recommended.

+1
source

Source: https://habr.com/ru/post/1386670/


All Articles