WinForm WebBrowser 控件加载缓慢。预加载的最佳方法?

发布于 2024-09-06 22:08:27 字数 280 浏览 1 评论 0原文

我们的 winform 应用程序中有一个摘要视图,它使用 WebBrowser 控件来显示网页中的摘要信息。我们使用它来获得一些丰富的表格布局,这是使用标准 winform 控件无法实现的。第一次在客户端上显示视图时,需要一段时间才能启动(我假设在后台启动 IE),并且应用程序冻结了相当多的几秒钟。我正在考虑某种预加载机制,可以让 IE 在后台启动,并且需要一些社区指导。

我正在考虑在应用程序启动时使用网络浏览器控件启动屏幕外或不可见的表单,或者在应用程序启动时在某种侧线程中启动 IE 实例。

有什么建议吗?

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 some 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兮子 2024-09-13 22:08:30

您可以在应用程序启动期间在单独的线程中预加载控件。
请注意 STA 单元状态,如果您想要操作某些 COM 对象(如 Web 浏览器),则该状态是必需的。

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();
        }
    }

}

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();
        }
    }

}
故人的歌 2024-09-13 22:08:30

我没有经历过您提到的启动滞后,但作为一个想法,在启动时您可以导航到 about:blank。然后,在浏览器的 DocumentCompleted 事件中,您可以开始导航到正确的 URL。这可能会帮助您了解到底是您的“IE 启动”占用了所有时间,还是内容查询和渲染占用了时间。

就您的架构而言,除非您的“丰富的表格布局”已经构建并且功能齐全,否则您可能会考虑使用 WPF(它也有一个很好的 WinForms 集成故事),以便在您的应用程序中获得一些更丰富、更性感的控件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文