We have a summary view in our winform app that uses a WebBrowser control to display the summary information from a web page. We are using this to gain som开发者_如何学运维e rich tabular layout that we could not accomplish using standard winform controls. the first time the view is displayed on the client it takes a while to spin up (launching IE in the background i assume) and the app freezes for quite a few seconds. I'm considering some sort of pre-loading mechanic that would get IE spun-up in the background and would like some community guidance.
I'm considering launching an off-screen or invisible form with a webbrowser control at app start or launching an IE instance in some sort of side thread at app start.
any suggestions?
You can preload the controls during the application startup, in a separate thread. Note the STA apartment state, which is mandatory if you want to manipulate some COM objects (like the WebBrowser).
public static class ControlPreloader
{
public static void PreloadAsync()
{
var thread = new Thread(Preload);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
private static void Preload()
{
using (var hostForm = new Form())
{
var control = new WebBrowser();
hostForm.Controls.Add(control);
control.CreateControl();
hostForm.Close();
}
}
}
I've not experienced the start up lags you mention, but as an idea, on start-up you could navigate to about:blank
. Then on browser's DocumentCompleted
event you could kick off a navigation to your proper URL. That might help you understand whether it's your "IE spin-up" that's taking all the time or the content query and render.
In terms of your architecture, unless of course your "rich tabular layout" is already built and fully functional then you might consider using WPF (it has a good WinForms integration story too) in order to get some richer and sexier controls into your app.
精彩评论