-1

Hi together i am Korbinian and i investigate currently a CPU cosumption problem regarding WPF and animated UI elements. We animate a UI element via System.Windows.Media.Animation.DoubleAnimation methode BeginAnimation. I am quite new to WPF especially animation.

Timeline.DesiredFrameRateProperty.OverrideMetadata(typeof(Timeline),
                new FrameworkPropertyMetadata { DefaultValue = 8 });

            flashAnimation = new DoubleAnimation(1, 0.3, TimeSpan.FromMilliseconds(330), FillBehavior.Stop)
            {
                RepeatBehavior = RepeatBehavior.Forever,
                AutoReverse = true

            };

            this.BeginAnimation(FlashOpacityProperty, flashAnimation);

hint: I already tried in the snippet above to reduce the DesiredFrameRate to 8 in order to reduce the CPU consumption.

As far as i can see during debugging, the FlashOpacityProperty's Callback (PropertyChangedCallback) is called around 8 times per second. This amount of callbacks is somehow independent from what is defined in the constructor of the DoubleAnimation. My Goal is to reduce the amount of callbacks. For my use case its not important to have a fluent and nice looking animation. It should mainly support for highlighting.

Is is possible to reduce the amount of callbacks? Or is this a fix kind of animation from .Net?

Thank you very much in advance

korbif
  • 1
  • 3
  • Each frame, the dp value will be recalculated by your animation. If you have a property change callback then it's going to get called each time the property changes. Reducing the frame rate will of course reduce the callbacks. Seeing as how this is opacity then I think you could reduce the frame rate even more with little to no perceptible difference. – Andy Mar 16 '23 at 15:46
  • So, i will try to explain it a bit more: The PropertyChangedCallback "OnFlashOpacityChanged" is calling an Action "FlashOpacityChanged". This action is an event which sets the opacity of every registered "AssociatedObject". So as i understand you, the opacity should somehow be set by the DoubleAnimation and not by the PropertyChangedCallback or? – korbif Mar 17 '23 at 07:18
  • Show us the relevant parts of your code. The opacities of the "associated objects" could be data-bound to the FlashOpacity property. It is unclear why exactly you have this PropertyChangedCallback and what exactly it is doing. It should however be clear that it must be called whenever the property value changes. – Clemens Mar 17 '23 at 08:33
  • hi, sorry for the late Reply. Thank you for your answer, this really helped me understand the concept behind that. I analyzed further the code which I have to maintain and at the end I found out that the way it was implememted fired around 8 property changed events per second. This was the main problem. I solved it by rewrite these parts, especially the part with handling the animation. – korbif Mar 21 '23 at 12:29

1 Answers1

0

The problem was, that the DependencyProperty was changed in an endless loop (~8 times per second).

Independent from that, the DoubleAnimation was not using the updated value from the DependencyProperty.

At the end, i removed the DependencyProperty and handled the animation in the certain Behavior implementation.

korbif
  • 1
  • 3