views:

665

answers:

4

EDIT - After playing around with a bunch of potential solutions (using backgroundworker and separate threads) I've found the key issue here is getting the data binding to be 'interrupted'. Since the progress bar is an animated circle (not a percent complete) it needs to respond to a timer event at consistent intervals to smoothly animate (which is why I can't data bind one by one and then send a 'progress' update back to the ui thread). Does anyone know of away to allow an animation to take place while data binding is going on? Thanks again for everyone's input!

Original Q -

I bind a large dataset to a WPF list box which can take a long time (more than ten seconds). While the the data is being bound I'd like to display a circular progress bar

I can't get the progress bar to show while the data binding is occurring, even though I am trying to do the binding in a backgroundworker. I tested it by making the first line of the backgroundworkd's dowork event a Thread.Sleep(5000) and sure enough the progress bar started spinning for that duration only to freeze while when the binding started.

Is this because both the databinding and the UI updating have to occur on the same thread? Any ideas on how to work around it?

Thanks for your help!!

A: 

The binding will occur on the UI thread, so you should update your progress bar through the BackgroundWorker, and use Dispatcher.Invoke to get it onto the UI thread.

Charlie
The circular progress bar I'm using (see link in Q) uses a DispatchTimer to update on the ticks for smooth animation. Since it doesn't work by a percentage complete, how should I go about making sure the timer interrupts the binding so the progress bar animates while the binding is occurring? Thanks for your help!
evan
Hmm, when you invoke onto the UI thread you can set a DispatcherPriority. If that priority is low, like Background, I don't think it will interrupt the binding. But if it is high I think it should take precedence and do that work first. I am not 100% sure but I will play around with it.
Charlie
+1  A: 

A good introduction to working with the WPF Dispatcher and a progress bar can be found in the CodeProject article: WPF ProgressBar

The article is quite short, but provides a great starting point for updating a WPF UI while work needs to be done in the background.

Remember: By default, all work is done on the UI thread in WPF.

Metro Smurf
thanks, I'll read this!
evan
A: 

It's likely not to be the binding, but the creation of the controls for the binding - the rendering of the control.

Have you tried using a VirtualisingStackPanel as your ItemsPanel? If it's a rendering bottleneck you might find this improves things.

Dan Puzey
That would probably solve the whole the delay problem. However, I'm using a wrap panel which I don't think supports virtualization. I figured it would be much faster to just put a progress bar over the list than to overwrite a wrap panel. Looks like I was probably wrong :) (which happens a lot) - any ideas on where I can info on making a wrap panel support virtualization if it doesn't already? Thanks for your reply!
evan
Veer
thanks - still going to try one more time to take the easy way out and just make the slowness look cool, but it looks like I'll probably have to create an extended wrap panel and those links will help a lot!
evan
to anyone else reading this an interested in a warp panel with virtualization , i just found this http://www.codeproject.com/KB/static/VirtualizingWrapPanel.aspx I haven't checked it out, but it looks cool!
evan
A: 

Use an adorner layer and put your progress indicator in that- that will allow it to overlay the controls on the screen and still respond. Load your data on a back thread and periodically update the control you are displaying your results in (i would also recommend using a virtualizing stack panel as your item panel). This way you can keep the progress indicator in the foreground on a timer for animation and push the incoming data to the results view using the dispatcher and a delgate. We implemented a similar solution for a client (large amounds of data coming in via java web services and spooled to grid- progressive discolsure ;)) and it worked like a champ.

J Rothe
Thanks man, that sounds like exactly what I need. I'm going to test it out later tonight and I'll let you know how it works out.
evan