Detecting when a windows form has been initialised and has completely painted

466 views Asked by At

I'm looking to implement a quick and dirty way of measuring the UI performance of a set of winforms. My idea is to put in System.Diagnostic timing calls into the code base and detect when the time from when the page is requested to when it is completely drawn and available to the user. Is this even possible? Is there an initial event I can hook into and an event I can hook into when the form has completely? painted. I know there is a Paint event but this is when the form starts to draw but this isn't going to help me.

I'm a web developer dealing with some windows forms hence the obvious question.

1

There are 1 answers

1
Sriram Sakthivel On BEST ANSWER

You can start the Stopwatch exactly before executing the constructor of your Form or in the first line of your constructor.

It is a good idea to start the Stopwatch before the constructor, because when you start it inside the constructor, already base class constructor will have finished running, so you'll be excluding those timings if you decide to start it in the first line of the constructor.

Then stop the Stopwatch in Form.Shown event. It will be raised when the form is fully shown to the user.

Then use Stopwatch.Elapsed to read the time taken for the form to load.

As @HansPassant noted in comments, you may need to add this.Update(); in the Shown event handler so that we can make sure all children are painted fully.