Export (0) Print
Expand All
4 out of 17 rated this helpful - Rate this topic

Improving Microsoft .NET Compact Framework-based Application Form Load Performance

.NET Compact Framework 1.0
 

Neil Cowburn
Content Master Ltd

March 2002

Applies to:
    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)

Contents

Introduction
Measuring Performance
Reduce the Number of Method Calls
Create the Controls Top-down
More Information

Introduction

The 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.

CAUTION   Do 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 in the InitializeComponent method, you can no longer use the Forms Designer. If you do, your modifications will be lost. You should 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 Performance

To 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 to the InitializeComponent method, you can record how long it takes to initialize the controls on the form. For this, you can use the 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 Calls

One of the ways in which you can improve the performance of loading a form is by reducing the number of method calls 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 calls for setting these properties, like this:

    this.textBox1.Location = new Point(10,20);
    this.textBox1.Size = new Size(72,23);

This can be consolidated 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 nearly 9% over the Forms Designer generated code is achieved.

Create the Controls Top-down

Another method for improving performance is to initialize the controls in the control tree top-down. For example, if you have a panel control with many 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 in the InitializeComponent method by creating the controls top-down and re-parenting them resulted in a performance improvement of over 49% 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.

More Information

For additional information, please see the following resources:

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.