19

I am attempting to measure the performance for a WPF based application. Currently we have code that times how long it takes to add the content to the WPF render tree. At this point, control is returned to our program. The problem is that there is still a lag before content is displayed on the screen by WPF. For complicated rendering trees, this can be a matter of seconds.

Can you recommend a method to determine when WPF has completed rendering to the screen? I would like these tests to be fully automated and not rely on someone sitting around with a stopwatch.

[update]

Thanks for the suggestions so far.

I have tried waiting for the Loaded and ContentRendered events, but both fire before the content makes it to the screen.

It looks like others are having this issue. I have tried the steps suggested at http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/693fbedb-efa6-413e-ab66-530c6961d3fb/ but still haven't been able force my code to wait for the rendering to complete.

Brian Erickson
  • 945
  • 8
  • 18

4 Answers4

13

You may use this solution: http://www.japf.fr/2009/10/measure-rendering-time-in-a-wpf-application/

Jonathan ANTOINE
  • 9,021
  • 1
  • 23
  • 33
  • 2
    Abstract: Schedule an action at the dispatcher right after "Children.Add" with an priority "loaded" (one below the rendering): this.Dispatcher.BeginInvoke( DispatcherPriority.Loaded, new Action(() => { sw.Stop(); Debug.WriteLine("Render " + sw.ElapsedMilliseconds + " ms"); })); – uli78 Jul 17 '19 at 06:13
4

Did you take a look at the WPF Performance suite? You might be able to see the performance and pinpoint problematic code.

Emond
  • 50,210
  • 11
  • 84
  • 115
  • 1
    are u using that tool? whenever I try it just feels overwhelming and unhelpful to me :/ – Elad Katz Mar 31 '11 at 18:25
  • Indeed the docs are pretty bad so it is or can be quite hard to understand what is being shown but these tools are freely available and can give you good info. Of course there are other tools as well and you can also add your own measuring using the events from the other answers. – Emond Mar 31 '11 at 18:33
  • yeah, that was me who answered the other answer ;-) – Elad Katz Mar 31 '11 at 18:36
  • Yes. There are a lot of things in WPF Performance suite to evaluate what is going on in an app in so you can clean up performance. That's not what I'm looking for here. I'm looking for a way to check the application performance against our performance specifications which require images to be displayed in a given amount of time. – Brian Erickson Mar 31 '11 at 18:48
  • @ErnodeWeerd: What is the current alternative for WPF Performance Suite for profiling .NET 4.5 apps? I would very appreciate you if you take a look at my question [here](http://stackoverflow.com/q/33468572/3345644). – Alexander Abakumov Nov 02 '15 at 17:46
1

You should use the event ContentRendered of Window.

This will fire every time there's a full rendering sequence, and you can time that.

Elad Katz
  • 7,483
  • 5
  • 35
  • 66
  • That should work but ContentRendered is firing long before the image makes it to the screen. this.ContentRendered += (s, e) => { MessageBox.Show("content rendered fired"); }; – Brian Erickson Mar 31 '11 at 20:46
  • I'm thinking that the lag is after the document is sent to the video card for GPU rendering and my only choice it to start screen scraping. – Brian Erickson Mar 31 '11 at 21:00
1

You could end the timer after the control's Loaded event since that occurs after the Rendering is complete.

Rachel
  • 130,264
  • 66
  • 304
  • 490
  • Thanks. I tried that and the ContentRendered event. Both are firing a good amount of time before the content gets to the screen. – Brian Erickson Mar 31 '11 at 20:55