UI refresh freezes app while paints
This is a speculative question so no code provided.
I have an application that takes a large XML file and builds a view from this for editing by the user. The loading of the XML is carried out in a background worker and is then bound to the view in a pretty vanilla MVVM way via a tree view.
This all works very well and a nice wait cursor is shown while the worker loads the XML.
However when the XML file gets very big (20M) the UI will 开发者_JAVA百科hang during the painting of the new view. This is to be expected, but it does mean all of the visual cues that the app is still alive, freeze as well. At the moment the cursor is changed once the loading has finished in the worker just before the paint. Is there a 'paintingHasFinished' type event for a control or nifty trick that I could use as a handle so I can change my waiting cursor after this UI action?
You can find out when a View has finished being rendered by using the Dispatcher.
Try adding this:
this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() =>
{
Cursor = Cursors.Arrow;
}
));
This hooks into the dispatcher, asking it to call the callback when it's done loading, using an asynchronous call. The provided anonymous Action will get called and should set your cursor back to normal.
You could add the ViewModels you create one by one or in batches of x, using the dispatcher, so that the items get added gradually and will not overload the measure/layout thread at the end of the load operation. This will increase overall load time but also increase user experience.
Also make sure virtualization is turned on (virtualizing stack panel or column/row virtualization on a datagrid), this will significantly improve performance with large data sets.
精彩评论