views:

554

answers:

5

I have a Silverlight 2 application that has a "Loading Data..." message that contains an animation. When the web service returns the data and the data is loading in to an observable collection -- the animation freezes (this can be for for several seconds while the data is loading and give the impression that something moght be wrong.)

The observable collection that is databound to an items control in a view.

I assume what is happening is when the data is being added to the observable collection the visual tree is being built for the databound control - which happens on the UI thread.

Is there anyway for me to lower the priority of the data loading so the animation on the ui thread can continue -- even when there is a ton of data work being done on the UI thread?

thanks Michael

A: 

Your animation isn't running because your JS is dealing with the returned data. (Single threaded browser.)

Solution: your JS loading sw needs to yield to the browser periodically to enable the browser to do some animation.

See my SO answer http://stackoverflow.com/questions/787500/why-is-jquery-ajax-so-slow-on-ie7/787568#787568 for more details on how to do it.

Larry K
hi Larry,it is not the xap loading animation (which is in js) - it is an animation running in the silverlight app which is c#.thanksMichael
MIantosca
A: 

I have been using a Progress Control with Indeterminate state, and then put that in a control with a semi-transparent background over the top of the items control. I have not seen it stop animating while the web service call is made, the return objects are processed into a ViewModel and the ItemControl is rendering the updated view model.

I think you can customize the progress bar template in a pretty straightforward way as well as adding the Loading Data... message as a TextBlock. I have a Loading... message above the Progress control as well.

Tony Heupel
Also note: I have the visibility of the Loading control bound to a flag in my ViewModel.
Tony Heupel
the animation runs very smooth while it is waiting for the web service to return but once it does and the data is being loaded into the collection it freezes. I will try the progress bar to see if its animation freezes.
MIantosca
A: 

Hiya,

You're right in saying that the callback happens on the UI thread. To solve your issue you can make it asynchronous so that it happens on a background thread and then build a new observable collection and replace the existing one in one operation. For example:

private SynchronizationContext _uiThread = SynchronizationContext.Current;

public void UpdateUIAsynchronously(ICollection<Data> data)
{
ObservableCollection<Data> temp = new ObservableCollection<Data>();
foreach(Data currentData in data)
{
temp.Add(currentData);
}

_uiThread.Post(delegate(object o) { myDataGrid.ItemsSource = temp }, null);
}

In my development I found that for a DataGrid I could just clear and add the new elements on the UI thread without performance hit. Maybe you could refactor your UI code?

R4cOON
+1  A: 

I don't know if you got the answer for this or not but here i go.

Try using a BackgroudWorker to put the heavy duty instructions in there.

Here is a very good example: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

Let me know if you have any issues, or let me know if you already solved this problem.

A: 

Do you find an answer to the problem ?? I'm having the same problem.

Carlos

related questions