0

I've got a WPF application. Most of the time, the app uses almost no CPU. This is good. But I have this one window that pops up under specific circumstances. According to the performance analyses I've run in Visual Studio, my code isn't using alot of CPU, the Application.Run method is. And most of that is in "Functon Body". To the tune of like 40-50% of the CPU.

How do I figure out what's using up the CPU? I've got some animations using key frames that animate the color of some things. I'm wondering if they're what's eating up the CPU. I know I can remove them temporarily, and I just might, but is there another way to figure out exactly what's eating the CPU?

Tony

PS: To prove to myself it was the animations, I watched the CPU usage of my program in Task Manager. It was about 0 before the window with the animations was displayed. The window displayed & the CPU went up. I then pushed a button which changes the state of the data & has the side effect of stopping the animations. The CPU went back down to 0. I repeated this a few times with consistent results. Looks like it's the animations to me. Though there is a possibility of it being something else that is also going on at the same time as the animations.

The animations are key frame animations intended to simulate a flashing light. I'm going to dump the key frame animations and just swap the fill brush color between two colors using a timer.

PS: If anyone is interested, I did a little research into WPF animation and I found out that there are at least 2 different Color Animation KeyFrame classes in WPF. The one I was using, LinearColorKeyFrame, continuously varies the color from whatever it is to whatever you want to be over the duration of the animation. The other is DiscreteColorKeyFrame. This instantly changes the color from whatever it is to whatever you want it to be.

So I edited my animation from this:

<Storyboard x:Key="FlashingStoryboard" AutoReverse="True" RepeatBehavior="Forever">
    <ColorAnimationUsingKeyFrames BeginTime="00:00:00" 
                                  Storyboard.TargetName="Flasher" 
                                  Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
        <LinearColorKeyFrame KeyTime="00:00:00.5" 
                             Value="{Binding Path=FlashColor, RelativeSource={RelativeSource AncestorType={x:Type local:Flasher}}}"/>
    </ColorAnimationUsingKeyFrames>
    <DoubleAnimation Duration="00:00:00.5" 
                     From="0" To="10" 
                     Storyboard.TargetName="FlasherBlur" 
                     Storyboard.TargetProperty="Radius">
    </DoubleAnimation>
</Storyboard>

To this:

<Storyboard x:Key="FlashingStoryboard" AutoReverse="True" Duration="00:00:00.5" RepeatBehavior="Forever">
    <ColorAnimationUsingKeyFrames BeginTime="00:00:00" 
                                  Storyboard.TargetName="Flasher" 
                                  Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
        <DiscreteColorKeyFrame KeyTime="00:00:00.25"
                               Value="{Binding Path=FlashColor, RelativeSource={RelativeSource AncestorType={x:Type local:Flasher}}}" />
    </ColorAnimationUsingKeyFrames>
</Storyboard>

The new animation uses a lot less CPU. It turns out that most of the CPU usage being hogged by the DoubleAnimation, which was producing a glowing effect. It used up almost 1/2 of the CPU! Not worth it at all. Even when I removed that, the CPU usage was still averaging about 15%. Switching it to a DiscreteKeyFrame brought it down under 10%.

There was another, similar animation animating a border around items in a ListBox in the same window. I made the same changes and now the CPU usage is acceptable. I'm not exactly sure what it is, but it's way better than it was.

Thanks.

4

2 回答 2

2

yep, animation, is the first thing that anyone will tell you. also how big is it? Is it non stop? relatively big chunk of the screen then yeah!

于 2012-04-20T18:49:47.243 回答
0

You need to do code profiling and see what consumes most CPU resources and maybe tune that way.

You can read more about .NET profiling here

于 2012-04-20T18:50:55.033 回答