. NET Compact frameworkimproving Microsoft. NET Compact framework-based application form load performance Neil cowburn Content master Ltd March 2002 Applies: Microsoft. NET Compact Framework 1.0 Microsoft Visual Studio. NET 2003 Summary: Learn how to reduce the time it takes for your. NET Compact framework Windows Forms applications to load by following some simple optimization techniques. (4 printed pages) ContentsIntroduction Measuring performance Reduce the number of method CILS Create the controls top-down More information IntroductionThe default forms designer generated code does not always create the most optimized code for creating Microsoft Windows Forms. however, there are a number of things you can do to optimize the generated code. by writing your own form initialization code, you can improve form load performance. Also, by re-arranging and/or rewriting the code generated by the forms designer in Microsoft Visual Studio. NET, the overall form load performance of your applications can be greatly improved.
CautionDo not use the designer after modifying initializecomponent.
The initializecomponent method has a comment preceding it, warning you not to modify the code. By modifying the code inInitializeComponent Method, you can no longer use the forms designer. if you do, your modifications will be lost. you shoshould only perform these optimizations as the final stage of development, after all the design work has been done.
This article assumes you have a working knowledge of the Microsoft. NET Compact framework and Microsoft Visual C #. net, and that you have installed Visual Studio. NET 2003. Measuring performanceTo measure the performance of your form initialization code, you can use a simple test to measure how long it takes to initialize the controls in a form. by recording the system timer's tick count before and after making the call toInitializeComponent Method, you can record how long it takes to initialize the controls on the form. For this, you can use Environment.TickCount Property: // Form constructor public Form1() { uint startTickCount, endTickCount, timeTaken; // Call GetTickCount to get the starting tick count startTickCount = Environment.TickCount; // Initialize the controls on the form InitializeComponent(); // Call GetTickCount again to get the end tick count endTickCount = Environment.TickCount; // Calculate the time taken (in ms) to initialize the controls timeTaken = startTickCount – endTickCount; // Display the time taken in a message box MessageBox.Show("Load Time: " + timeTaken.ToString() + "ms"); } Reduce the number of method CILSOne of the ways in which you can improve the performance of loading a form is by referencing the number of methods CILS made during form initialization. for example, the code generated by the forms designer for setting the location and size of a control uses two method CILS for setting these properties, like this: this.textBox1.Location = new Point(10,20); this.textBox1.Size = new Size(72,23); This can be called lidated into a single method call by using the bounds property: this.textBox1.Bounds = new Rectangle(10,20,72,23); To show the performance gains that are possible, consider a form which contains 1 menubar, 1 tabcontrol control, 5 tabpages each containing 7 labels, 7 buttons and 7 textboxes. that's a total of 112 controls. this application was built using the release profile and deployed to the Pocket PC 2002 emulator, where it was run 5 times. when the method call optimization described above is applied, a performance improvement of nearly9%Over the forms designer generated code is achieved. Create the controls top-downAnother method for improving performance is to initialize the controls in the control tree top-down. for example, if you have a panel control with your controls in it, create the Panel first, and then add the controls to the Panel. also, setting the Parent property of the control instead of adding to the controls collection can improve performance. for example, consider adding a textbox to a panel's control collection: // Before optimization // Create a new panel and textbox control Panel panel1 = new Panel(); TextBox textBox1 = new TextBox(); // Set the Text property of the TextBox control textBox1.Text = "My Text"; // Add the TextBox to the Panel's control collection panel1.Controls.Add(this.textBox1); // Add the Panel to the Form's control collection this.Controls.Add(panel1); ... // Add subsequent controls here Optimizing this code snippet using the top-down and parenting techniques results in the following snippet: // After optimization // Create a new panel and textbox control Panel panel1 = new Panel(); TextBox textBox1 = new TextBox(); // Parent the Panel to the current Form this.panel1.Parent = this; // Parent the TextBox to the Panel this.textBox1.Parent(this.panel1); // Set the Text property of the TextBox control textBox1.Text = "My Text"; ... // Add subsequent controls here Together these can make a big difference with a deeply nested control hierarchy. Optimizing the code inInitializecomponentMethod by creating the controls top-down and re-parenting them resulted in a performance improvement of over49%Over the default forms designer generated code. Combining method call optimization with the top-down control initialization technique gave an overall performance boost of approximately 55% over the forms designer generated code. this is an incredible improvement and if you want to boost the form loading performance of your application, it is worthwhile making these Optimizations to your code |