Ticket Q422245
Visible to All Users

WinForms slow first load of form

created 13 years ago

I've noticed this for a long time since I've used DevExpress… but never really try to tackle it till now…

The problem is that in a Winforms App… I notice that if I opened a form within the app (say it's an MDI app)… the first time that form will take forever to load. However, if I closed that form and then reopen it again, it would be must faster relative to the first load. This happens for each form that's in the application.

I've tried to initialize each form in the application by doing new… to now avail!
I've tried ngen install myapp.exe … no difference
I've tried… and still no go…

private static void PreLoadDlls()
          {
               foreach (var asm in AssemblyHelper.GetDevExpressAssemblies()) // Get all devexpress assemblies referenced
               {
                    RunTypeInitializers(Assembly.Load(asm));
               }

RunTypeInitializers(Assembly.GetEntryAssembly());
               RunTypeInitializers(Assembly.GetExecutingAssembly());
          }

static void RunTypeInitializers(Assembly a)
          {
               Type[] types = a.GetExportedTypes();
               for (int i = 0; i < types.Length; i++)
               {
                    RuntimeHelpers.RunClassConstructor(types[i].TypeHandle);
               }
          }

What is it that is taking so long on the first load?

Thanks

Kim

Answers

created 9 years ago (modified 9 years ago)

Put the active solution platform on x86 (if it doesn't exist --> new --> not base it on Any CPU)

Sped my program up x10

Also you can add:

Call after InitializeComponent():

C#
private static void PreJitControls() { ThreadPool.QueueUserWorkItem((t) => { Thread.Sleep(1000); // Or whatever reasonable amount of time try { foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) { foreach (var method in type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)) { System.Runtime.CompilerServices.RuntimeHelpers.PrepareMethod(method.MethodHandle); Console.WriteLine(method.Name); } } } catch (Exception) { } }); }

to further increase the speed!

    Comments (2)
    DevExpress Support Team 9 years ago

      Hello Evi,

      Thank you for sharing your findings with others. I would like to add that the DevExpress components installer creates native images for all DevExpress assemblies and installs them on your machine. It does this only for x86 platform. Perhaps, this is why the startup time decreased when you changed the platform to x86 in your project settings. If so, I am afraid that this effect will be lost when you deploy the application.

      However, you can do the same as our installer does. You can create the native image for your application executable and all referenced assemblies when deploying your application. This will significantly decrease the application startup time. Refer to the following MSDN articles for details:
      Ngen.exe (Native Image Generator)
      NGen: Getting Started with NGen in Visual Studio

      Also, see my answer in the following ticket:  How to speed up loading of a custom pop-up form.

      SP SP
      Stephen Pefanis 9 years ago

        I Cannot express how happy i am to find this solution. Chaning to x86 made a MASSIVE difference

        created 13 years ago (modified 13 years ago)

        Hi Kim,
        In fact, when the form is loaded for the first time, it requires more time than when the form is loaded for the second time. Since ngen does not help (whereas it should!), please try to profile the application and check which processes require so much time. I am afraid we cannot explain the cause of the issue without a sample project showing the glitch.
        Update
        Perhaps the cause of the issue was discussed in the devexpress components tent to connect to internet before starting thread. Please try the solution posted there. Does this help?

          Show previous comments (14)

            Pretty much it… can you imagine the user waiting 6 seconds to open a form??? We might as well go ASP.NET!!! And host it on same poor old server!!!

              Plato,
              We really need your help here.

              DevExpress Support Team 13 years ago

                Hi,
                I see the problem with the new form. You have created a huge form that cannot be loaded fast by default. If I were you, I would do the following:

                1. The first TabPage content should be moved to a separate UserControl, and this UserControl should be added dynamically to the TabPage.
                2. The second page is the most heavy. I would remove a BarManager and all toolbars from the UserControl and create a single instance of them on the form. All you need to do is to switch the RichEditBarController's Control property to the currently focused RichEdit.
                  I also think that additional time is requested by loading images from the RichEditCore dll for the BarManager. If you remove these images, it will be spent less time for opening the form.
                  Finally, since your forms are really heavy, I suggest that you use the SplashScreenManager to invoke the WaitDialog when it is necessary. Attached is an updated sample. Does it work faster on your machine?

                Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

                Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.