多次打开多个网页后,经常会冻结

问题描述

我正在开发一个Web搜寻器,它将经常请求新的网页。在测试过程中,程序经常冻结。

我写了一个测试用例,解释了我的用法和出现问题的情况。

有时,当它打开一个网页10次时,可能会在它打开20次时卡住。我不确定打开网页有多少次。

using CefSharp;
using CefSharp.WinForms;
using System;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        ChromiumWebbrowser browser;

        System.Timers.Timer timer;
        int i = 1;

        public Form1()
        {
            InitializeComponent();

            timer = new System.Timers.Timer(400) { AutoReset = true };//Timer,automatically open a new web page
            timer.Elapsed += Timer_Elapsed;

            browser = new ChromiumWebbrowser("www.baidu.com") { Parent = splitContainer1.Panel1,Dock = DockStyle.Fill };
            browser.LifeSpanHandler = new LifeSpanHandler(splitContainer1.Panel2);
            browser.FrameLoadEnd += browser_FrameLoadEnd;
        }

        private void browser_FrameLoadEnd(object sender,FrameLoadEndEventArgs e)
        {
            if (e.Frame.IsMain == false) return;
            timer.Start();
        }

        private void Timer_Elapsed(object sender,System.Timers.ElapsedEventArgs e)
        {
            i = i > 1 ? 1 : i + 1;
            browser.ExecuteScriptAsync("document.getElementsByClassName('mnav c-font-normal c-color-t')[" + (i) + "].click()");//Open a link in the web page
        }
    }


    class LifeSpanHandler : ILifeSpanHandler
    {
        Control host;

        internal LifeSpanHandler(Control host)
        {
            this.host = host;
        }

        public bool DoClose(IWebbrowser chromiumWebbrowser,Ibrowser browser)
        {
            return false;
        }

        public void OnAfterCreated(IWebbrowser chromiumWebbrowser,Ibrowser browser)
        {

        }

        public void OnBeforeClose(IWebbrowser chromiumWebbrowser,Ibrowser browser)
        {

        }

        public bool OnBeforePopup(IWebbrowser chromiumWebbrowser,Ibrowser browser,IFrame frame,string targetUrl,string targetFrameName,WindowOpendisposition targetdisposition,bool userGesture,IPopupFeatures popupFeatures,IWindowInfo windowInfo,IbrowserSettings browserSettings,ref bool noJavascriptAccess,out IWebbrowser newbrowser)
        {
            newbrowser = null;

            var webbrowser = (ChromiumWebbrowser)chromiumWebbrowser;
            webbrowser.Invoke(new Action(() =>
            {
                if (host.HasChildren) host.Controls[0].dispose();
                var control = new Control { Parent = host,Dock = DockStyle.Fill };
                control.CreateControl();
                windowInfo.SetAsChild(control.Handle);
            }));

            return false;
        }
    }
}

测试代码放置在GitHub上,这是Visual Studio 2019项目。我们希望您可以在本地执行项目,以便更直观地发现问题。非常感谢。

https://github.com/haohaodz/cefsharp-form-vs2019-problem.git

解决方法

您是否已与其他网站进行了测试?

对其他网站的频繁请求也会导致死锁。

问题是否通过github.com/cefsharp/CefSharp.MinimalExample重现

该问题不会在github.com/cefsharp/CefSharp.MinimalExample中重现。

在BrowserTabUserControl.cs

browser.LifeSpanHandler = new LifeSpanHandler(openPopupsAsTabs: false);

默认情况下会在新窗口中打开网页。频繁打开多个网页,不会重现冻结问题。

我将openPopupsAsTabs更改为true,然后在标签页中打开新页面

browser.LifeSpanHandler = new LifeSpanHandler(openPopupsAsTabs: true);

频繁打开多个网页,程序将冻结。 程序冻结在dispose方法中:

System.Windows.Forms .dll! System.Windows.Forms . UnsafeNativeMethods.DestroyWindow ( 
     System.Runtime.InteropServices . handleref hWnd) unknown
System.Windows.Forms .dll! System.Windows.Forms . NativeWindow.DestroyHandle () unknown
System.Windows.Forms .dll! System.Windows.Forms . Control.DestroyHandle () unknown
System.Windows.Forms .dll! System.Windows.Forms . Control.Dispose (bool disposing) unknown
System.dll ! System.ComponentModel.Component . dispose() unknown

有时候程序突然退出,有一个例外:

Exception thrown:“ System.IO.PipeException ”(located at System.ServiceModel.dll (2)
Exception thrown:“ System.ServiceModel.CommunicationException ”(located at System.ServiceModel.dll (2)
Exception thrown:“ System.ServiceModel.CommunicationException ”(located at System.ServiceModel.dll (2)
Exception thrown:“ System.ServiceModel.CommunicationException ”(located at System.ServiceModel.dll (2)
Exception thrown:“ System.ServiceModel.CommunicationException ”(located at System.ServiceModel.Internals . DLL)
Exception thrown:“ System.ServiceModel.CommunicationException ”(located at System.ServiceModel.Internals . DLL)
Exception thrown:“ System.ServiceModel.CommunicationObjectFaultedException ”(located at System.ServiceModel.dll (2)