Getting around the WPF rendering bottleneck
I love to make efficient apps and often look to concurrency and multithreading to improve application responsiveness and such, but lately my attempts always seem to be blocked by WPF's single-threadedness. No matter how efficient and parallel my code is, WPF seems to continually stall my UI and make my app look incredibly unresponsive--sometimes waiting for a window to render ca开发者_如何学JAVAn take precious seconds (where even the mouse cursor won't move). I find this frustrating as it seems there's nothing I can do to speed things up.
My question, then, is is there anything I can do to improve responsiveness in my WPF app? My windows tend to be rather complex in style and composition, and I am already using virtualizing StackPanels.
To illustrate my problem: I have a text box that acts as a search box, and I want results displayed on the fly as the user types (each result is encapsulated in a UserControl and displayed in an ItemsControl). I am using background threads to perform the search. The problem is, it's when WPF renders the results of the search that completely stalls the UI so that typing is frozen for seconds at a time while WPF churns away, making the whole "results as you type" thing not very viable.
Is there any way I can avoid having the UI thread stall while WPF is rendering?
Given that you're already using threads to fetch data I can only surmise that the data sets being returned are large enough to be causing the slowdown. (Could you provide some metrics there?) For example, you may have a thread fetching an array of names but if the result is in the many, many thousands then there will definitely be a performance hit when you bind.
The only thing strategy I can think of is to narrow the results coming back from your thread (using the TOP sql clause if you're doing Sql Server) or, if you really need to display all the data that is coming back, break up the stream into chunks so that the UI does not have to render as much data all at once. The latter option would be rather complex and might call for creating from scratch or at least overriding the UI controls.
精彩评论