I have the following problem:
I want to write a WPF application which needs to do heavy multithreaded computation in the background. Right now I just add Threads to the Threadpool and if the find the right answer I call the Dispatcher.BeginInvoke methode to update the UI. So at times I have more than 1000 Threads in my Threadpool which wait to be excecuted and the UI freezes.
So I think I need a way to be able to have a fast Queue which runs only a fixed number of Threads while keeping one thread for the UI which always is at idle, so the user can still interact with it.
Anyone got an good Idea on how to solve this? Or do I have to implement a ThreadQueue myself which does excactly that?
My Current Code in Pseudo Code:
WPF Aplication
init(){
BackgroundWorker.dosomething();
}
updateUI(data){
Dispatcher.BeginInvoke(()=>{
UIFrame = data
})
}
BackgrondWorker
dosomething(){
for(i = 0; i < 50000; i++){
ThreadPool.QueueUserWorkItem(dosomethingElse);
}
}
dosomethingElse(){
//Heavy computation with data as a result
if(foundsomthing){
WPF.updateUI(data);
}
}