WPF animation freezes even if I use a new theme

I ran into a big problem: in my WPF application, MainWindow contains a border with loading animations (storyboard). By default, it crashed. Sometimes I make it visible and crash when I load a lot of data or when I load a new XAML screen.

First of all, I did not use Threading at all and the animation, where both freeze and appear with the latter.

Then I started using Threading as follows:

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 for 1 second, and then it just hung until all the data and the new displayed screen were loaded. A This is how the main thread blocks ALL threads.

I tried adding a timer to delay my Messenger.StopAnimation() up to 3 seconds later. Even this freezes my animation for 1 second, when everything changes and loads onto my new screen, and then my animation lasts for 3 seconds.

I tried Dispatcher , BackgroundWorker and ran the same issue as with the previous code.

I tried to include the animation in a popup and even in a new window that was transparent. Nothing to do, it always ends with freezing for 1 second until it stops ...

For information, I use an ObservableCollection (I tried with List, the same problem), and I load different screens inside the ContentControl in my MainWindow .

I watched the Techdays, Fast, and Furious videos, and I really wanted to get smooth animation like the video, but the freeze seems impossible to remove.

+4
source share
2 answers

I had the same problems loading 50+ small images in a simple collection. In the end, I found out that the .NET Framework, which draws bitmaps on the screen, is what binds the main thread, and that I had absolutely no way around this. I tried to put all the elements in the view, but slowly populated the collection associated with the view, but every time the observed collection was changed, the .NET Framework redrawed all the images in the view, so that didn't help either.

I asked a Microsoft engineer about this, and he directly admitted that this is a problem in the .NET Framework, which should be fixed by Microsoft in the future. One of my colleagues said that you can get around the problem by drawing all the pixels yourself with your own algorithms.

I assume that if you want to fix this, you will have to change the way your data is displayed, although I cannot help you with the solution.

+1
source

We have the same problem as Jack, with a ListView binding to an ObservableCollection. After much analysis, we decided that the sorting and grouping process was slow, not a query in the database. Finally, we implemented a separate thread, but, of course, he had to call the dispatcher to sort and group, which returned this lengthy operation to the main user interface thread - so it looks like we started our nice animation “Loading ..” that appears for hanging up and being jerky is not what we wanted at all ... As Akku said, I don’t see a solution for this, except changing the way the data is displayed.

0
source

Source: https://habr.com/ru/post/1444113/


All Articles