I'm doing performance tuning on a Silverlight WP7 game and I'm observing a weird behavior when running in the Emulator (I have no real device, so I can't tell if it's an emulator artifact).
I am using a Storyboard as main game loop timer, I update the rendering in the Completed event, then restart the animation. The problem is that the Completed event is triggered at very different intervals.
I isolated the problem in a very simple function, called for ex. on a button click (I put this in a totally empty application, there is nothing else going on in the app).
var sw = new Stopwatch();
var animation = new Storyboard();
animation.Duration = TimeSpan.FromMilliseconds(10);
animation.Completed += (s, e) =>
{
sw.Stop();
Debug.WriteLine(sw.ElapsedMilliseconds.ToString());
sw.Reset();
sw.Start();
animation.Begin();
};
animation.Begin();
The output I would expect (ideally) is something like
10
10
10
10
...
but instead, this is what I get:
12
15
16
17
17
14
10
10
10
9
11
132
10
20
11
11
10
12
The time between cycles varies quite a bit, but most important there are occasional very long delays (like the 132 above). This happens regardless of the animation Duration, i.e. the actual duration is "centered" towards the set duration, but varies a lot with intermittent long delays.
You can imagine that my game is running quite irregularly, not smooth and uniform at all. Also I noticed that regardless of how simple the render operation is, I cannot go over ~42 FPS.
Question 1: Am I doing some obvious and blatant mistake? Is my approach flawed?
Question 2: Why is the Storyboard duration so inconsistent between runs?