Fulg
Fulg

Reputation: 43

WPF animation freezes even if I use new Thread

I am facing a big issue : In my WPF app, the MainWindow contains a Border with a Loading Animation (storyboard). By default it's collapsed. I sometimes make it visible and collapsed when I load a lot of data or when I'm loading a new XAML screen.

First of all, I wasn't using Threading at all and the animation where both freezing and appearing with lates.

Then I began to use Threading like this :

Messenger.StartAnimation();
var task = Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(150);
                }).ContinueWith((a) =>
                {
                    // HERE Screen moving + large amount of data loaded with Entity Framework
                    Thread.Sleep(200);

                    Messenger.StopAnimation();
                }, CancellationToken.None, TaskContinuationOptions.NotOnFaulted, threadUIContext);

This time, the animation worked during 1 second and then it just freezed until all data and new displayed screen where loaded. A It's like the main thread blocks ALL threads.

I've tried to add Timer to Delay my Messenger.StopAnimation() to 3 seconds later. Even this will freeze my animation for 1 second when everything is being changed and loaded on my new screen and then my animation continues for 3 seconds.

I've tried Dispatcher, BackgroundWorker and met same problem that with previous code.

I've tried to put my animation on a popup and even on a new Window which was Transparent. Nothing to do, it always end up freezing for 1 second before stopping...

For info, I use ObservableCollection (I've tried with List, same problem) and I load different screens inside a ContentControl in my MainWindow.

I've watched the techdays video, Fast and Furious and I really wanted to get a smooth animation like on the video, but the freeze seems impossible to remove.

Upvotes: 4

Views: 2548

Answers (2)

ausadmin
ausadmin

Reputation: 1698

We have the same problem as Jack, with a ListView binding to an ObservableCollection. After lots of analysis we decided it was the process of sorting and grouping that was slow, not the query on the database. Finally we implemented a separate thread, but of course had to call the dispatcher for the sorting and grouping which put that long running operation back on the main UI thread - so that appears to have swamped our nice "Loading.." animation - which appears to the user to hang and be jerky - not what we wanted at all.... As Akku stated, I don't see a solution to this, other than changing the way the data is displayed.

Upvotes: 0

Akku
Akku

Reputation: 4454

I had the same issues when loading 50+ small images in a simple collection view. I found out eventually, that the .NET Framework drawing the bitmaps to the screen is what ties up the main thread, and that I had absolutely no way of circumventing that. I tried putting all the elements in the view but slowly filling the collection which was bound to the view, but every time the observable collection got changed, the .NET Framework redrew all the images in the view, so this didn't help too.

I asked a Microsoft engineer about this, and he bluntly admitted that it's a problem in the .NET Framework that will have to be fixed by Microsoft in the future. A colleague of mine said you could circumvent the issue by drawing all pixels yourself with your own algorithms.

I imagine if you want to fix this you'll have to change the way your data gets displayed, although I canot help you with the solution.

Upvotes: 1

Related Questions