Home / Community & Support / Knowledge Base / RadControls for WinForms / Themes and Styling / Load theme components in a separate thread

Load theme components in a separate thread

Article Info

Rating: 5

Article information

Article relates to

RadControls for WinForms, Q2 2009 SP1 

Created by

 Nikolay Diyanov

Last modified

 Sep 23, 2009

Last modified by

 Nikolay Diyanov



PROBLEM

When you register/load a theme in your project, this registration takes some time when you start your application. If you have a large number of themes, this time may become noticeable. If you application initially starts with only one theme applied to the RadControls on the form (for example ControlDefault theme), this means that you do not need to have the other themes loaded when the application is started. So, in order to descrease application loading time we can load the other themes in a separate thread.

SOLUTION

In this article we will demonstrate what is the benefit of loading themes in a separate thread on form load. Let's make a small application of one RadForm and a RadComboBox. By RadComboBox we will set the ApplicationThemeName property.

Let's first load the predefined theme components in the main thread: 
public Form1()  
{  
    InitializeComponent();  
     
    LoadThemeComponents();           
}  
 
void LoadThemeComponents()  
{  
    new Office2007BlackTheme();  
    new Office2007SilverTheme();  
    new AquaTheme();  
    new BreezeTheme();  
    new MiscellaneousTheme();  
    new DesertTheme();  
    new TelerikTheme();  
    new VistaTheme();  

Please note that when we make new instances of the theme components we actually register the themes that are contained in these components.

In order to measute the time of the form load, we set  the following code in the Program.cs:
[STAThread]  
static void Main()  
{  
    int StartTime = System.Environment.TickCount;  
    Application.EnableVisualStyles();  
    Application.SetCompatibleTextRenderingDefault(false);  
 
    Form1 form = new Form1();  
    form.Shown +=  
        delegate 
        {  
            MessageBox.Show((System.Environment.TickCount - StartTime).ToString());  
            //Application.Exit();  
        };  
    Application.Run(form);  

Let's now start the application in Release mode. We get an average timespan of 1422 ms.

Let's create the theme component objects in a separate thread. We will do this with the help of a BackgroundWorker in the Form_Load event handler:
private void Form1_Load(object sender, EventArgs e)  
{  
    BackgroundWorker worker = new BackgroundWorker();  
    worker.DoWork += delegate { this.LoadThemeComponents(); };  
    BeginInvoke(new MethodInvoker(worker.RunWorkerAsync));  

In this case we get an average timespan of 890 ms. This result compared to the 1422 ms means that we decrease the loading time with about 532 ms. The good thing about loading themes in different thread is that they are already loaded when we want to use them, for example after the form is loaded, I select "Office2007Black" from the RadComboBox and I get the Office2007Black style successfully loaded in the application.


Please find attached a sample project below which demonstrates the described approach.

Comments

If you'd like to comment on this KB article, please, send us a Support Ticket.
Thank you!

Please Sign In to rate this article.