1

I have a loading animation user control, LoadingAnimation.xaml, which is included in another user control called Loading.xaml.

A part of the LoadingAnimation.xaml:

    <UserControl x:Class="Universal_updater._1_Presentation.Control.LoadingAnimation"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         xmlns:system="clr-namespace:System;assembly=mscorlib"
         xmlns:local="clr-namespace:Universal_updater._1_Presentation.Control"
         Height="Auto" Width="Auto">
      <UserControl.Resources>
        <system:String x:Key="txtLoading">Loading...</system:String>
        <Storyboard x:Key="ProgressAnimation" RepeatBehavior="Forever">
        <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="block" Storyboard.TargetProperty="(UIElement.OpacityMask).(SolidColorBrush.Color)">
            <SplineColorKeyFrame KeyTime="00:00:00" Value="Black"/>
            <SplineColorKeyFrame KeyTime="00:00:00.2290000" Value="#EF000000"/>
            <SplineColorKeyFrame KeyTime="00:00:00.4590000" Value="#E2000000"/>

Loading.xaml:

    <UserControl x:Class="Universal_updater._1_Presentation.Loading"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:control="clr-namespace:Universal_updater._1_Presentation.Control"
         mc:Ignorable="d" 
         d:DesignHeight="376.5" Width="736
         ">
      <Grid>
        <Label x:Name="lbLoading" Margin="-101,-57,101,57"></Label>
        <control:LoadingAnimation x:Name="LoadingAni" HorizontalAlignment="Center" VerticalAlignment="Center" Panel.ZIndex="10"/>
        <Rectangle x:Name="rcLock" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="376" Stroke="Black" VerticalAlignment="Top" Width="736" Opacity="0.7"/>       
      </Grid>
    </UserControl>

The Loading.xaml is used in my window as following:

    <local:Loading x:Name="loadingWin" HorizontalAlignment="Center" VerticalAlignment="Center" Panel.ZIndex="10" Visibility="{Binding Path=IsVisible, Source={x:Static local:PropertySettings.Instance}}"  IsEnabled="{Binding Path=IsActive, Source={x:Static local:PropertySettings.Instance}}" />

The Loading animation is controlled by static variables in my code behind:

    public async void UpdateScreen()
    {
        //Activating Loading animation
        PropertySettings.Instance.IsVisible = Visibility.Visible;
        PropertySettings.Instance.IsActive = true;

        await Task.Run(() =>
        {
            Thread.Sleep(4000);
        });

            //Deactivating Loading animation
            PropertySettings.Instance.IsVisible = Visibility.Collapsed;
            PropertySettings.Instance.IsActive = false;

    }

The functionality is working pretty fine. The loading animation is made visible and it does collapse after the task is finished.

However before i call the async method UpdateScreen() the cpu usage is 0 %. When the loading animation is visible and active, the cpu usage gets to 19%. The cpu usage gets to 9% after the loading animation is collapsed and disabled, which is supposed to be 0%.

I did run WPF perfomance suit, which resulted in the rendering thread is still working and uses much cpu.

I did try to disable the loading animation inside a Application.Current.Dispatcher.BeginInvoke but with no luck. The cpu is still high:

     Application.Current.Dispatcher.BeginInvoke(new Action(() =>
        {
            //Deactivating Loading animation
            PropertySettings.Instance.IsVisible = Visibility.Collapsed;
            PropertySettings.Instance.IsActive = false;
        }));

Why is the cpu usage for the program not getting to zero percent after collapsing it?

Rhys
  • 4,511
  • 2
  • 23
  • 32
  • It's because you've not told the animation to stop ever. It may be collapsed, but the animation is still going. You will need to instruct it to remove / pause the storyboard once it's collapsed/content has been loaded. – Logan Jan 25 '16 at 10:18
  • *"rendering thread is still working and uses much cpu"* - wpf is smart enough to not render invisible element, it can't be rendering of this element, but perhaps something else. You are using `RepeatBehavior="Forever"` and never [stop](http://stackoverflow.com/q/20298/1997232) `ProgressAnimation`, this is simply *not smart* in case of loading animation. – Sinatr Jan 25 '16 at 11:22
  • Thanks for the helpful comments. I got it fixed by stopping the animation. It was a stupid mistake from my side. – user3000138 Jan 25 '16 at 11:27

0 Answers0