0

I'm having a major performance issue regarding the first load of a WPF control embedded in a Forms UserControl, it takes between 3 and 4 seconds just to add an ElementHost object at runtime.

I am using Visual Studio 2008 for a .net 3.5 app.

I have the following setup:

A Forms container with a single Panel:

using System.Windows.Forms;
using System.Windows.Forms.Integration;

namespace MyNamespace
{
    public partial class TestContainer : UserControl
    {
        private ElementHost ctrlHost;
        private TestWPFControl wpfAddressCtrl;

        public TestContainer()
        {
            InitializeComponent();
            ctrlHost = new ElementHost();
            ctrlHost.Dock = DockStyle.Fill;
            panel1.Controls.Add(ctrlHost); //slow!
            wpfAddressCtrl = new TestWPFControl();
            wpfAddressCtrl.InitializeComponent();
            ctrlHost.Child = wpfAddressCtrl;
        }
    }
}

A standard VS2008 generated WPF control:

namespace MyNamespace
{
    public partial class TestWPFControl : UserControl
    {
        public TestWPFControl()
        {
            InitializeComponent();
        }
    }
}

Accompanying XAML:

<UserControl x:Class="MyNamespace.TestWPFControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="382" Width="467">
<Grid>
    <Button Height="23" Margin="170,46,222,0" Name="button1"   VerticalAlignment="Top">Button</Button>
</Grid>

The WPF control (for testing purposes) only has 1 single Button, no logic or anything.

Once I Create an instance of the TestContainer, it takes roughly 3 to 4 seconds for it to show the WPF.

While debugging the above, the line

panel1.Controls.Add(ctrlHost); 

is the slowing factor.

Profiling with Timer:

new ElementHost(): 23ms 
ElementHost.DockStyle: 0ms
'C:\Windows\assembly\GAC_MSIL\WindowsFormsIntegration\3.0.0.0__31bf3856ad364e35\W‌​indowsFormsIntegration.dll' 
Panel.Controls.Add(ElementHost): 4130ms

The loading of W‌​indowsFormsIntegration.dll seems to take ~4 seconds on every cold boot.

This is my first time trying to add a WPF control to a Forms element and I've no idea what is causing, let alone how to solve the huge first-load time of adding an ElementHost to a forms control.

For the record, the second time I create a new instance of the TestContainer is fast as expected.

Karrok
  • 115
  • 1
  • 4
  • 13
  • http://stackoverflow.com/questions/22774663/elementhost-size-causes-slow-wpf-open-load-with-high-memory-usage and http://stackoverflow.com/questions/14586975/how-can-i-prevent-garbage-collection-from-being-called-when-calling-showdialog-o/14605424#14605424 – StepUp Feb 27 '16 at 18:18
  • Both those questions/answers do things I don't use though. I don't load an icon anywhere which seemed to be the major issue in the two other questions. – Karrok Feb 27 '16 at 18:23
  • 25 miliseconds my computer: `Stopwatch sw = new Stopwatch(); sw.Reset(); sw.Start(); ctrlHost = new ElementHost(); ctrlHost.Dock = DockStyle.Fill; panel1.Controls.Add(ctrlHost); sw.Stop(); MessageBox.Show(sw.ElapsedMilliseconds.ToString());` *.net 3.5* – NoName Feb 28 '16 at 06:19
  • new ElementHost(): 23ms ElementHost.DockStyle: 0ms 'C:\Windows\assembly\GAC_MSIL\WindowsFormsIntegration\3.0.0.0__31bf3856ad364e35\WindowsFormsIntegration.dll' Panel.Controls.Add(ElementHost): 4130ms Maybe that loading of WindowsFormsIntegration is a HW problem then or something? – Karrok Feb 28 '16 at 09:25

0 Answers0